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

吐槽一下python的行内 if 表达式

  •  
  •   love · 2013-04-04 11:10:43 +08:00 · 11738 次点击
    这是一个创建于 4011 天前的主题,其中的信息可能已经有所发展或是发生改变。
    感觉二个分支结果的重要性是不对等的,比如

    port = 8080 if settings.DEBUG else 80

    8080靠=符,视觉上是正常的情况,而80在最后,似乎是非正常情况,但很多时候二个分支是平等的。
    23 条回复    1970-01-01 08:00:00 +08:00
    Js
        1
    Js  
       2013-04-04 12:15:33 +08:00   ❤️ 4
    port = [80, 8080][settings.DEBUG]
    这样就对等了.....
    binux
        2
    binux  
       2013-04-04 12:19:01 +08:00
    port = settings.DEBUG ? 8080 : 80
    你就开心了?
    TankyWoo
        3
    TankyWoo  
       2013-04-04 13:15:09 +08:00
    要想80感觉正常,8080感觉非正常,就
    port = 80 if not settings.DEBUG else 8080
    reorx
        4
    reorx  
       2013-04-04 13:41:31 +08:00
    有个一般不用的做法,不过可能楼主会喜欢: port = settings.DEBUG and 8080 or 80

    注意 and 后面的值的布尔值不能为 False,否则它的结果就和 ? : 不完全一样了。
    archsocks
        5
    archsocks  
       2013-04-04 18:15:28 +08:00
    @TankyWoo 要反写条件感觉不爽,且要多打一个not

    实在想不通python把其中一个值放到条件之前是出于什么目的啊!人为地制造不平等。

    我觉得这样就很好:
    port = if (settings.DEBUG) 8080 else 80
    GTim
        6
    GTim  
       2013-04-04 18:18:52 +08:00
    @Js 这个方法不错
    Hualin
        7
    Hualin  
       2013-04-04 19:11:27 +08:00
    英语国家的人就是那么说话的,你看到的数学书证明部分 if else 也是布置的。正经事不干瞎操心啥 =.=
    wenbinwu
        8
    wenbinwu  
       2013-04-04 19:51:41 +08:00
    settings.DEBUG and 8080 or 80
    jimrok
        9
    jimrok  
       2013-04-05 22:16:15 +08:00
    ruby port = if settings.DEBUG
    8080
    else
    80
    end
    Parallel
        10
    Parallel  
       2013-04-05 22:21:20 +08:00
    我想吐槽一下那个列表推导式,就是那个轻量级的循环。。全部挤在一行,个人觉得一点也不美观。。
    reusFork
        11
    reusFork  
       2013-04-05 22:34:43 +08:00
    @Parallel 可以写成几行的
    rephaslife
        12
    rephaslife  
       2013-04-05 22:40:57 +08:00   ❤️ 1
    反正我觉得挺正常的,Python目的是“像写英语一样写代码”。

    @Parallel 其实这样挺好的,简洁,就是必须在[]里使用有些蛋疼。。比如下面的代码就会出错:
    {a for a in list: a}
    就算这样也会报错:
    {a for a in list}
    Parallel
        13
    Parallel  
       2013-04-06 10:43:23 +08:00
    @reusFork 你是说在[]里写成几行那样?
    @rephaslife 我觉得在里面写可读性不强,就比如《可爱的python》里那个计算闰年的程序result2 = [ p for p in range(2, N) if 0 not in [ p% d for d in range(2, int(sqrt(p))+1)] ]。
    reusFork
        14
    reusFork  
       2013-04-06 11:13:51 +08:00
    @Parallel 是的,像这样
    Parallel
        15
    Parallel  
       2013-04-06 12:11:01 +08:00
    @reusFork 这样也行吗,学习了~
    hexor
        16
    hexor  
       2013-04-06 12:31:00 +08:00
    条件语句请不要写成一行啊... 混蛋
    swulling
        17
    swulling  
       2013-04-06 12:33:39 +08:00
    @archsocks port = 80 if settings.DISABLE_DEBUG else 8080

    条件哪有正写反写之分。。
    swulling
        18
    swulling  
       2013-04-06 12:37:45 +08:00
    @Parallel 实际用起来超好用的,如果你非常讨厌这种写法,会变的有点丑。
    result2 = [ p for p in range(2, N) if 0 not in [ p% d for d in range(2, int(sqrt(p))+1)] ]

    ---
    result2 = []
    for p in range(2,N):
    ____tmp = []
    ____for d in range(2,int(sqrt(p))+1)):
    ________tmp.append(p%d)
    ____if 0 not in tmp:
    ________result2.append(p)
    holsety
        19
    holsety  
       2013-04-06 12:46:44 +08:00
    还有一种写法是:

    port = settings.DEBUG and 8080 or 80

    当然,8080不能为False,None等非True值,要不然上面这个不成立.
    Parallel
        20
    Parallel  
       2013-04-06 12:51:42 +08:00
    @swulling 事实上只需要这样就可以了:
    result1 = []
    for num in range(2, N):
    __for snu in range(2, int(sqrt(num))+1):
    ____if num % snu == 0:
    ______break
    __else:
    ____result1.append(num)
    可能是我不大习惯那样子吧,也许以后习惯了就好。
    brucex
        21
    brucex  
       2013-04-06 13:03:03 +08:00
    @rephaslife 你没有理解列表推导的实质,(a for a in list)会产生一个可迭代对象,你可以把它当成是一个生成器,如果你要得到一个Dict,你应该这样写{(a,a) for a in list)},如果要得到一个Set,应该写作set(a for a in list)。
    brucex
        22
    brucex  
       2013-04-06 13:08:31 +08:00
    @brucex 得到Dict也应该是dict((a,a) for a in list),上面是错的。
    Lelouchcr
        23
    Lelouchcr  
       2013-04-06 18:40:31 +08:00
    @Js 长见识了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2672 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:46 · PVG 23:46 · LAX 08:46 · JFK 11:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.