推荐学习书目
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 · 4447 views
    This topic created in 4442 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
        1
    VeryCB  
       Apr 10, 2014
    貌似只能 singers or singers.Jagger
    RIcter
        2
    RIcter  
       Apr 10, 2014
    没有三目(或者类似于)这样的简单的方法
    只有a = b if b else c这样..
    zqqf16
        3
    zqqf16  
       Apr 10, 2014   ❤️ 1
    a = b and b.c
    skydiver
        4
    skydiver  
       Apr 10, 2014
    Python 连三目运算符都没有呢。。。别指望这个了
    ryan10107
        5
    ryan10107  
       Apr 11, 2014
    @skydiver python可以用and or达到三目运算符的效果。这是DIP给出的 (1 and [a] or [b])[0] ,可以看看这位博客的解释http://blog.chinaunix.net/uid-24673811-id-2005968.html
    skydiver
        6
    skydiver  
       Apr 11, 2014
    @ryan10107 题主说的也是语法糖。不是不能实现,没有语法糖而已
    wuhang89
        7
    wuhang89  
       Apr 11, 2014
    python没有三目运算
    davidli
        8
    davidli  
       Apr 11, 2014
    在Python中, ?貌似只能出现在#后或者''' '''内部
    mengzhuo
        9
    mengzhuo  
       Apr 15, 2014
    @skydiver

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

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

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