推荐学习书目
› 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
josephok
V2EX  ›  Python

Python 里有没有这种简单的方法?

  •  
  •   josephok · Apr 10, 2014 · 4765 views
    This topic created in 4604 days ago, the information mentioned may be changed or developed.
    CoffeeScript中有一个Existential Operator(?号)。比如:singers?.Jagger表示singers有值时访问Jagger属性。Python中是否有这种简便方法?
    11 replies  •  1970-01-01 08:00:00 +08:00
    VeryCB
    VeryCB
    Apr 10, 2014
    1
    貌似只能 singers or singers.Jagger
    RIcter
    RIcter
    Apr 10, 2014
    2
    没有三目(或者类似于)这样的简单的方法
    只有a = b if b else c这样..
    zqqf16
    zqqf16
    Apr 10, 2014❤️1
    3
    a = b and b.c
    skydiver
    skydiver
    Apr 10, 2014
    4
    Python 连三目运算符都没有呢。。。别指望这个了
    ryan10107
    ryan10107
    Apr 11, 2014
    5
    @skydiver python可以用and or达到三目运算符的效果。这是DIP给出的 (1 and [a] or [b])[0] ,可以看看这位博客的解释http://blog.chinaunix.net/uid-24673811-id-2005968.html
    skydiver
    skydiver
    Apr 11, 2014
    6
    @ryan10107 题主说的也是语法糖。不是不能实现,没有语法糖而已
    wuhang89
    wuhang89
    Apr 11, 2014
    7
    python没有三目运算
    davidli
    davidli
    Apr 11, 2014
    8
    在Python中, ?貌似只能出现在#后或者''' '''内部
    mengzhuo
    mengzhuo
    Apr 15, 2014
    9
    @skydiver

    三目不就是
    x = 1 if y else 0 吗
    skydiver
    skydiver
    Apr 15, 2014
    10
    @mengzhuo 我指的是问号冒号运算符
    mengzhuo
    mengzhuo
    Apr 15, 2014
    11
    @skydiver

    这两者不就是等价的三元运算符?

    x = 1 if y else 0
    x = (y == True) ? 1:0
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4469 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 06:53 · PVG 14:53 · LAX 22:53 · JFK 01:53
    ♥ Do have faith in what you're doing.