V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chenqh
V2EX  ›  Python

有没有关于 py3 type hint 的最佳实现

  •  
  •   chenqh · 2018-05-22 09:29:22 +08:00 · 2813 次点击
    这是一个创建于 2137 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如调用远程 http 请求如何把返回的 dict 使用 type hint?使用 Dict[]还是使用类?

    18 条回复    2018-05-23 00:03:32 +08:00
    menc
        1
    menc  
       2018-05-22 12:29:44 +08:00   ❤️ 1
    调用远程 HTTP 是没有 dict 返回类型的,如果你说的是 json,那么用 json 库来 parse
    chenqh
        2
    chenqh  
    OP
       2018-05-22 13:34:39 +08:00
    @menc 但是怎么自动转化成类呢,像 typescript as 一下就好了,就变成类了,python 里面一定要 Cls.__init__吗?
    vicalloy
        3
    vicalloy  
       2018-05-22 14:12:08 +08:00
    python 里的是 type hint,只是给 IDE 用的。
    对你来说主要问题是你用库不支持 type hint,你只要“告诉 IDE ”变量的类型就可以了。
    比如:
    from typing import Mapping
    import json
    s = json.loads("""{'s': 'd', 's': 3}""")
    b: Mapping = s
    menc
        4
    menc  
       2018-05-22 14:17:07 +08:00
    menc
        5
    menc  
       2018-05-22 14:17:54 +08:00   ❤️ 1
    chenqh
        6
    chenqh  
    OP
       2018-05-22 15:53:14 +08:00
    @menc 我想要的不是这个,怎么说,我想要的是 IDE 智能感觉 dict 里面有哪些 key,就像个类一样
    menc
        7
    menc  
       2018-05-22 16:12:16 +08:00
    @chenqh



    那我猜测,你要的是这个?
    menc
        8
    menc  
       2018-05-22 16:16:23 +08:00   ❤️ 1
    @chenqh
    如果你要写类,倒不如用 protobuf 和 thrift 了,写好 IDL,类自动生成的
    chenqh
        9
    chenqh  
    OP
       2018-05-22 16:17:35 +08:00
    @menc 有没有根据 json 自动生成类的呀,感觉 js 对 json 转变成类比 python 要好呀
    menc
        10
    menc  
       2018-05-22 16:21:46 +08:00
    @chenqh 自动生成类也是动态的。。在 ide 层面还没运行的时候实现代码提示是不可能的。。js 也是不可能的。。
    chenqh
        11
    chenqh  
    OP
       2018-05-22 16:28:22 +08:00
    @menc 不是,刚刚我其实说的是两个问题,第一个就是根据一个 json 例子,generate 类的代码<br/>
    2. typescript 的 json.parse 返回的本身就是一个对象,而且根据鸭子模式,只要字段一致,就可以直接当成某个特殊的类来用,不像 python,json.loads 返回的是一个 dict,dict 和类之间有明显的区别
    sun1991
        12
    sun1991  
       2018-05-22 16:38:30 +08:00   ❤️ 1
    首先定义好 class, 然后自己写个方法, 从 dict 映射到 class. 大不了就是 hasattr 和 setattr...
    Arnie97
        13
    Arnie97  
       2018-05-22 16:57:27 +08:00 via Android
    @chenqh #11 那是必然的,Python 里面 dict 和 object 完全是两样不同的东西,JS 里面都是 object …
    Arnie97
        14
    Arnie97  
       2018-05-22 17:04:00 +08:00 via Android   ❤️ 1
    @sun1991 #12

    from collections import OrderedDict

    class AttrDict(OrderedDict):
    __getattr__ = OrderedDict.__getitem__ __setattr__ = OrderedDict.__setitem__
    chenqh
        15
    chenqh  
    OP
       2018-05-22 17:37:04 +08:00
    @Arnie97 这个东西有什么作用?
    menc
        16
    menc  
       2018-05-22 19:08:03 +08:00   ❤️ 1
    @chenqh


    this ?
    虽然没什么卵用,但是我想实现了你说的事情了
    bnm965321
        17
    bnm965321  
       2018-05-22 19:35:20 +08:00
    看 mypy 文档就懂了
    chenqh
        18
    chenqh  
    OP
       2018-05-23 00:03:32 +08:00
    我发现我想要的最接近我想要的是 py3.7 dataclass 或者 attrs 这个库的功能了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1539 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:11 · PVG 01:11 · LAX 10:11 · JFK 13:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.