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

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

  •  
  •   haoliang · 2022-09-01 08:25:37 +08:00 · 4028 次点击
    这是一个创建于 602 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2022-09-02 13:06:58 +08:00
    haoliang
        1
    haoliang  
    OP
       2022-09-01 08:26:55 +08:00
    abersheeran
        2
    abersheeran  
       2022-09-01 08:34:05 +08:00
    本来就是用来简化

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

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

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

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

    ```
    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  
       2022-09-01 18:32:25 +08:00
    一般没使用. 只用在 if 上. 其他情况不用.用了看代码反倒复杂了..
    有时候 if 后需要返回值在去加个 : 号 方便.免得重新起一行代码
    haoliang
        12
    haoliang  
    OP
       2022-09-01 19:21:50 +08:00
    看起来大家对我的暴论无感,谢谢大家的支持!
    DOLLOR
        13
    DOLLOR  
       2022-09-01 23:10:39 +08:00
    @BingoXuan
    感觉不会有了,python 圈子大多不推崇 lambda
    V2SINE
        14
    V2SINE  
       2022-09-02 13:06:58 +08:00
    对于我这种不喜欢手动开关 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()
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   974 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:41 · PVG 05:41 · LAX 14:41 · JFK 17:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.