Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

if object has property python

Use hasattr()

hasattr(object, name)
#The arguments are an object and a string.
#The result is True if the string is the name of one of the
#object’s attributes, False if not.
#(This is implemented by calling getattr(object, name) and
#seeing whether it raises an AttributeError or not.)

Docs: https://docs.python.org/3/library/functions.html#hasattr
Comment

python3 check if object has attribute

try:
    doStuff(a.property)
except AttributeError:
    otherStuff()
Comment

check if an object has an attribute in Python

if hasattr(a, 'property'):
    doStuff(a.property)
else:
    otherStuff()
Comment

check if an object has an attribute in Python

assert hasattr(a, 'property'), 'object lacks property' 
print(a.property)
Comment

check if an object has an attribute in Python

getattr(a, 'property', 'default value')
Comment

PREVIOUS NEXT
Code Example
Python :: python with statement file does not exist exception 
Python :: python decimal to string 
Python :: tkinter messagebox 
Python :: TypeError: cannot unpack non-iterable int object 
Python :: Python NumPy swapaxis Function Example 2 
Python :: python get first day of year 
Python :: python close file 
Python :: python aws s3 client 
Python :: Python program to check Co-Prime Number 
Python :: python manage.py collectstatic --noinput 
Python :: roots of quadratic equation in python 
Python :: how to make a pause in python 
Python :: internal server error 500 python flask 
Python :: how to slice even index value from a list in python using slice function 
Python :: python open file relative to script location 
Python :: python refresh import 
Python :: basic calculator in python 
Python :: round up division python 
Python :: opencv dilate 
Python :: python Pyramid Patterns 
Python :: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable 
Python :: convert string to integer in dictionary python 
Python :: how to make label background transparent in tkinter 
Python :: get rid of unnamed column pandas 
Python :: time.sleep() faster 
Python :: python print color 
Python :: how to find the datatype of a dataframe in python 
Python :: pandas backfill 
Python :: how to change column name in pandas 
Python :: keras example 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =