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

PEP435加入了Enum支持……我就问下我们为什么需要Enum?有哪些典型的应用场景?

  •  
  •   lsj5031 · 2013-09-28 06:17:10 +08:00 · 3237 次点击
    这是一个创建于 3860 天前的主题,其中的信息可能已经有所发展或是发生改变。
    8 条回复    1970-01-01 08:00:00 +08:00
    11
        1
    11  
       2013-09-28 06:42:42 +08:00
    比如,出现 typo 的时候。
    humiaozuzu
        2
    humiaozuzu  
       2013-09-28 09:49:02 +08:00 via iPhone
    函数多种返回结果的时候
    miaoever
        3
    miaoever  
       2013-09-28 09:52:09 +08:00
    比如写自动机需要定义各种状态的时候。
    lsj5031
        4
    lsj5031  
    OP
       2013-09-28 10:16:56 +08:00
    @humiaozuzu
    @miaoever
    之前用tuple或者dict甚至自定义的class不也挺方便吗?
    humiaozuzu
        5
    humiaozuzu  
       2013-09-28 10:20:07 +08:00
    @lsj5031 dict 可以,不过语法真的不爽,目前都是用的 class 实现的,不过有 native 的方法更好吧
    BOYPT
        6
    BOYPT  
       2013-09-28 15:33:56 +08:00
    以往很多pythoner认为不需要ENUM特性,哈哈哈
    zhujinliang
        7
    zhujinliang  
       2013-09-29 17:53:41 +08:00
    以前
    bool DoSomething()
    {
    ...
    }

    接触C#多了以后:
    enum DoSomethingResult
    {
    Success,
    Failed
    }
    DoSomethingResult DoSomething()
    {
    ...
    }
    Ricepig
        8
    Ricepig  
       2013-09-30 09:19:33 +08:00 via iPhone
    @zhujinliang 当出现第三种返回状态时,enum就显得方便了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5338 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 09:11 · PVG 17:11 · LAX 02:11 · JFK 05:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.