Check element exists in array

后端 未结 6 826
说谎
说谎 2021-01-30 08:23

In PHP there a function called isset() to check if something (like an array index) exists and has a value. How about Python?

I need to use this on arrays because I get \

6条回答
  •  太阳男子
    2021-01-30 08:59

    You may be able to use the built-in function dir() to produce similar behavior to PHP's isset(), something like:

    if 'foo' in dir():  # returns False, foo is not defined yet.
        pass
    
    foo = 'b'
    
    if 'foo' in dir():  # returns True, foo is now defined and in scope.
       pass
    

    dir() returns a list of the names in the current scope, more information can be found here: http://docs.python.org/library/functions.html#dir.

提交回复
热议问题