To check the existence of a local variable:
if 'myVar' in locals(): # myVar exists.
To check the existence of a global variable:
if 'myVar' in globals(): # myVar exists.
To check if an object has an attribute:
if hasattr(obj, 'attr_name'): # obj.attr_name exists.
Related Posts:
- Manually raising (throwing) an exception in Python
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- How to print an exception in Python?
- What is the naming convention in Python for variable and function names?
- python exception message capturing
- Proper way to declare custom exceptions in modern Python?
- Append integer to beginning of list in Python
- Catch multiple exceptions in one line (except block)
- Catch multiple exceptions in one line (except block)
- Importing variables from another file?
- How to properly ignore exceptions
- How to check if type of a variable is string?
- django MultiValueDictKeyError error, how do I deal with it
- How can I print variable and string on same line in Python?
- Viewing all defined variables
- Printing variables in Python 3.4
- Multiple try codes in one block
- Copy a list of list by value and not reference
- Python NameError, variable ‘not defined’
- Getting the name of a variable as a string
- What is the purpose of the single underscore “_” variable in Python?
- Handling a timeout error in python sockets
- Strange Exception in Tkinter callback
- Set variable in jinja
- How can I safely create a nested directory in Python?
- TypeError: only size-1 arrays can be converted to Python scalars (matplotlib)
- Why do I get TypeError: can’t multiply sequence by non-int of type ‘float’?
- Writing a pandas DataFrame to CSV file
- Anaconda vs. miniconda
- No module named serial
- ImportError: No module named psycopg2
- DataFrame constructor not properly called
- How does functools partial do what it does?
- What does pythonic mean?
- Python strip with \n [duplicate]
- ImportError: Missing required dependencies [‘numpy’]
- Why is it string.join(list) instead of list.join(string)?
- How to prevent errno 32 broken pipe?
- How to iterate over rows in a DataFrame in Pandas
- How do I concatenate two lists in Python?
- if A vs if A is not None:
- TypeError: ‘function’ object is not subscriptable – Python
- How can I make an EXE file from a Python program?
- Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
- What is dtype(‘O’), in pandas?
- ctypes – Beginner
- Keras, how do I predict after I trained a model?
- How do I find out my PYTHONPATH using Python?
- ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)
- Overflow Error in Python’s numpy.exp function
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock
- Python Finding Prime Factors
- ValueError: ‘object too deep for desired array’
- How to create an array of bits in Python?
- Setting Django up to use MySQL
- In Python, when to use a Dictionary, List or Set?
- How can I check for NaN values?
- Python – how to use the constant e^
- Check if two unordered lists are equal
- Multiple assignment and evaluation order in Python
- deleting rows in numpy array
- How to make a 3D scatter plot in matplotlib
- Arrays used as indices must be of integer (or boolean) type
- How can I compare two lists in python and return matches
- How to draw vertical lines on a given plot in matplotlib
- Changing the “tick frequency” on x or y axis in matplotlib
- Is there a list of Pytz Timezones?
- Flask example with POST
- How do I change the figure size with subplots?
- Clear terminal in Python [duplicate]
- ModuleNotFoundError: No module named ‘discord’
- No numeric types to aggregate – change in groupby() behaviour?
- “ImportError: No module named site” on Windows
- Algorithm for solving Sudoku
- How to read pickle file?
- Euclidean Algorithm / GCD in Python
- Pandas split DataFrame by column value
- Using numpy to build an array of all combinations of two arrays
- Get a random sample with replacement
- Can’t update to numpy 1.13 with anaconda?
- What is a “slug” in Django?
- Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll
- if else function in pandas dataframe
- Primality test in python [duplicate]
- Flask-SQLalchemy update a row’s information
- Inline for loop
- Read data (.dat file) with Pandas
- ‘instancemethod’ object has no attribute ‘__getitem__’ with class variables
- Python/Scikit-Learn – Can’t handle mix of multiclass and continuous
- Sorting list of lists by the first element of each sub-list
- Install py2exe for python 2.7 over pip: this package requires Python 3.3 or later
- What is the correct format to write float value to file in Python
- How is Python’s List Implemented?
- “public” or “private” attribute in Python ? What is the best way?
- How to search for a string in text files?
- TypeError: ‘zip’ object is not subscriptable
- Pip: could not find a version. No matching distribution found
- Compare two columns using pandas
- ValueError: total size of new array must be unchanged
- Shuffle an array with python, randomize array item order with python