检查多个变量是否为None的最pythonic方法是什么?

时间:2017-02-21 07:20:03

标签: python

如果我有这样的结构:

def foo():
    a=None
    b=None
    c=None

    #...loop over a config file or command line options...

    if a is not None and b is not None and c is not None:
        doSomething(a,b,c)
    else:
        print "A config parameter is missing..."

python中首选的语法是检查所有变量是否都设置为有用值?是我写的还是其他更好的方式?

这与此问题不同: not None test in Python ...我正在寻找检查许多条件是否为None的首选方法。我输入的选项看起来很长而且非pythonic。

6 个答案:

答案 0 :(得分:42)

你这样做的方式没有错。

如果您有变量,可以将它们放在列表中并使用all

if all(v is not None for v in [A, B, C, D, E]):

答案 1 :(得分:26)

真的可以简化很多事情

if None not in (a, b, c, d):
    pass

答案 2 :(得分:6)

我知道这是一个老问题,但是我想添加一个我认为更好的答案。

如果所有需要检查的元素都是可哈希的,则可以使用集合而不是列表或元组。

>>> None not in {1, 84, 'String', (6, 'Tuple'), 3}

这比其他答案中的方法快得多。

>>> import timeit
>>> timeit.timeit("all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])")
1.7880705000000034
>>> timeit.timeit("None not in [1, 84, 'String', (6, 'Tuple'), 3]")
0.35424169999998867
>>> timeit.timeit("None not in (1, 84, 'String', (6, 'Tuple'), 3)")
0.3454340999999772
>>> timeit.timeit("None not in {1, 84, 'String', (6, 'Tuple'), 3}")
0.09577370000002361

答案 3 :(得分:2)

针对OP提出的具体情况

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorDarkGrey"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:itemIconTint="@drawable/bottom_navigation_selector"
        app:itemTextColor="@drawable/bottom_navigation_selector"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

就足够了。

if not all([a, b, c])

如果缺少任何参数,则求值为False。

答案 4 :(得分:1)

写一个单独的答案,因为当添加为注释时我不知道如何格式化代码。

Eternal_N00B的解决方案与Daniel Roseman的解决方案不同。考虑例如:

>>> all(v is not None for v in [False])
True
>>> all([False])
False

答案 5 :(得分:0)

补充Daniel Roseman的回答,我想:

if all([a,b,c,d]):

更清洁

由于all()内置函数已遍历列表检查None

相关问题