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

Python 的海象操作符,整体使用起来感觉咋样?

  •  
  •   haoliang · Sep 1, 2022 · 5062 views
    This topic created in 1356 days ago, the information mentioned may be changed or developed.
    def fn_with_long_name(n):
        return n
    
    
    def case_1():
        if (x := fn_with_long_name(2)) == 2:
            print("a", x)
        else:
            print("b", x)
    
        # 什么?这里多了个变量 x ?
    
    
    def case_2():
        x = 100
    
        if (x := fn_with_long_name(2)) == 2:
            print("a", x)
        else:
            print("b", x)
    
        # 啊? x 在上面哪改了
        assert x != 100
    
    
    def case_3():
        x = fn_with_long_name(3)
        if x == 2:
            print("a", x)
        else:
            print("b", x)
    
    

    我觉得声明变量藏在条件语句中,这也太难找了吧, 一不留神多了个变量;更怕覆盖了之前的值,因为这种写法让我以为声明的变量的作用域是 if 块。 所以我的暴论是:我各种情况下一概不用,也不看它的适用场景了;毕竟 python zen 中说:

    • Explicit is better than implicit.
    • Special cases aren't special enough to break the rules.
    • There should be one-- and preferably only one --obvious way to do it.
    14 replies    2022-09-02 13:06:58 +08:00
    haoliang
        1
    haoliang  
    OP
       Sep 1, 2022
    abersheeran
        2
    abersheeran  
       Sep 1, 2022
    本来就是用来简化

    var = ......
    if var == .....:

    这种结构的,你来一句“我以为作用域在 if 块内”🤔
    Trim21
        3
    Trim21  
       Sep 1, 2022 via Android
    python 有 if 作用域吗…
    tairan2006
        4
    tairan2006  
       Sep 1, 2022
    python 本来就没有块作用域啊。。你这
    princelai
        5
    princelai  
       Sep 1, 2022
    用得很好,但是还挺好用的,除了条件语句变得很长,另外你这个情况为什么不用 match case 语句
    princelai
        6
    princelai  
       Sep 1, 2022
    @princelai #5 是用的很少
    NessajCN
        7
    NessajCN  
       Sep 1, 2022
    这不就是 c++基本操作
    BingoXuan
        8
    BingoXuan  
       Sep 1, 2022
    其实是为了计算同时声明变量并赋予值,列表推导式里面用的比较多。或者就是 lz 表达中在 if 里面使用。

    什么时候能匿名函数能写多行,这才是我最关心的。一天天整有的没的
    Virace
        9
    Virace  
       Sep 1, 2022
    挺好用的,就是要熟悉语法之后,就好了。
    变量名规范,自己也看得舒服,没什么毛病。

    真要从头 x 到尾然后说不好用,那只能说确实不适合这种情况= =
    mylifcc
        10
    mylifcc  
       Sep 1, 2022
    偶尔能用上,大多数情况不推荐,不容易控制作用范围,有时候要加括号

    ```
    if last_data := DataModel.objects.filter(
    time__lte=(now_beijing() - timedelta(days=days))).order_by('-time').first():
    last_value = last_data.value
    elif last_data := DataModel.objects.filter(
    time__gte=(now_beijing() - timedelta(days=days))).first():
    last_value = last_data .value
    else:
    ...

    ```
    llsquaer
        11
    llsquaer  
       Sep 1, 2022
    一般没使用. 只用在 if 上. 其他情况不用.用了看代码反倒复杂了..
    有时候 if 后需要返回值在去加个 : 号 方便.免得重新起一行代码
    haoliang
        12
    haoliang  
    OP
       Sep 1, 2022
    看起来大家对我的暴论无感,谢谢大家的支持!
    AV1
        13
    AV1  
       Sep 1, 2022
    @BingoXuan
    感觉不会有了,python 圈子大多不推崇 lambda
    V2SINE
        14
    V2SINE  
       Sep 2, 2022
    对于我这种不喜欢手动开关 buffer 的人来说挺实用的,比如



    ```python
    with tqdm(iterable) as pbar:
    for item in pbar:
    # do staff
    pbar.set_postfix(...)
    ```

    可以被简化成

    ```python
    for item in (pbar := tqdm(iterable)):
    # do staff
    pbar.set_postfix(...)
    ```

    当然,分开写也行,但我不太喜欢

    ```python
    pbar = tqdm(iterable)
    for item in pbar:
    # do staff
    pbar.set_postfix(...)
    pbar.close()
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1571 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 16:42 · PVG 00:42 · LAX 09:42 · JFK 12:42
    ♥ Do have faith in what you're doing.