1 Python Determines whether a variable is defined

Source: Internet
Author: User
Tags vars

1 ## Python Determines if a variable has been defined2 3 detect if a variable is defined in Python4 5 The first method uses the built-in function locals ():6 locals (): Get Dictionary of defined objects7 8 'TestVar'   inchlocals (). Keys ()9 Ten The second method uses the built-in function dir (): One dir (): Get a list of defined objects A  - 'TestVar'   inchdir () -  the The third method uses the built-in function VARs (): - VARs (): Get Dictionary of defined objects -  -VARs (). Has_key ('TestVar') +  -  +Examples of----------------applications--------------------------------- A  at #TestVar Not defined - Print 'TestVar' inchlocals (). Keys () - #Output: False -  - Print 'TestVar' inchdir () - #Output: False in  - PrintVARs (). Has_key ('TestVar')   to #Output: False +  - #define TestVar theTestvar=1 *  $ Print 'TestVar' inchlocals (). Keys ()Panax Notoginseng #Output: True -  the Print 'TestVar' inchdir () + #Output: True A  the PrintVARs (). Has_key ('TestVar')   + #Output: True -  $------------------------------------- $ ##说明: - if the defined function has arguments, but no arguments are called, other errors are reported, not the wrong variables undefined!!!  - deffunc1 (srcdir): the     PrintSrcdir -     Wuyi Try: the func1 () - except: Wu     Print "1111"

1 Python Determines whether a variable is defined

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.