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
cbsw
V2EX  ›  Python

Python type hint 被 PEP 接受了

  •  
  •   cbsw ·
    dengshuan · 2015-05-24 10:38:18 +08:00 · 5157 次点击
    这是一个创建于 3265 天前的主题,其中的信息可能已经有所发展或是发生改变。

    pep-0484

    看看泛型在 Python 中怎么玩:

    from typing import TypeVar, Generic
    
    T = TypeVar('T')
    
    class LoggedVar(Generic[T]):
        def __init__(self, value: T, name: str, logger: Logger) -> None:
            self.name = name
            self.logger = logger
            self.value = value
    
        def set(self, new: T) -> None:
            self.log('Set ' + repr(self.value))
            self.value = new
    
        def get(self) -> T:
            self.log('Get ' + repr(self.value))
            return self.value
    
        def log(self, message: str) -> None:
            self.logger.info('{}: {}'.format(self.name message))
    

    感觉这是在往静态语言演化啊

    15 条回复    2015-05-25 19:18:26 +08:00
    RIcter
        1
    RIcter  
       2015-05-24 10:49:25 +08:00 via iPad
    感觉和 swift 好像了…
    unity0703
        2
    unity0703  
       2015-05-24 11:23:01 +08:00
    想不明白,动态类型语言要范型干嘛
    unity0703
        3
    unity0703  
       2015-05-24 11:31:14 +08:00
    @unity0703 看原文中说目的有三点
    1、静态分析和重构
    2、运行时类型检查
    3、利用类型信息进行代码生成(也就是范型?)
    这真是要往静态语言演化了
    Septembers
        4
    Septembers  
       2015-05-24 11:36:55 +08:00 via Android
    @unity0703 有点看不明白Python的 类型系统 了
    davidlau
        5
    davidlau  
       2015-05-24 12:02:27 +08:00
    @Septembers 想做动静态语法都兼容,任由开发者选择,用于不同目的的系统开发?
    LPeJuN6lLsS9
        6
    LPeJuN6lLsS9  
       2015-05-24 12:41:04 +08:00
    幸好没有引入新关键词之类的东西,可以当它不存在。但是Cython之类扩展的“类型语法”和这个很不同,是不是会兼容这边呢……
    poke707
        7
    poke707  
       2015-05-24 12:51:10 +08:00 via Android
    不要像2 to 3要么不用要么强行妥协就可了
    像c++四大派也各玩各的互不影响
    cbsw
        8
    cbsw  
    OP
       2015-05-24 14:08:32 +08:00
    对现有代码没有任何影响,但新项目可以利用这个特性做一些类型检查,IDE 之类的用这个特性效率、准确性应该都会提高不少。
    @poke707 未来怎么演化就难说了
    @Livid 从 /new 入口发帖忘了选节点了,/new 默认是问答节点?能不能帮忙移到 /go/programmer/go/python
    Livid
        9
    Livid  
    MOD
       2015-05-24 14:11:50 +08:00
    @cbsw 如果没有选的话,默认是 /go/new

    现在已经移动到 /gp/python
    cbsw
        10
    cbsw  
    OP
       2015-05-24 14:12:29 +08:00
    @Livid 哇,神速啊,谢谢!
    poke707
        11
    poke707  
       2015-05-24 14:38:30 +08:00 via Android
    @cbsw 恩,现在想到的是,有了这个看别人代码时会很有帮助
    fy
        12
    fy  
       2015-05-24 17:44:00 +08:00
    这很好啊,便于解释器分析和代码补全
    kongkongyzt
        13
    kongkongyzt  
       2015-05-24 18:55:35 +08:00
    非常好啊!很喜欢
    hjie
        14
    hjie  
       2015-05-25 08:51:59 +08:00
    看起来不优雅
    cbsw
        15
    cbsw  
    OP
       2015-05-25 19:18:26 +08:00
    与 java annotation 功能差不多
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2250 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:39 · PVG 17:39 · LAX 02:39 · JFK 05:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.