Is there any way to check if a variable (class member or standalone) with specified name is defined? Example:
if "myVar" in myObject.__dict__ : # not an easy way
print myObject.myVar
else
print "not defined"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A compact way:
htw’s way is more Pythonic, though.
hasattr()is different fromx in y.__dict__, though:hasattr()takes inherited class attributes into account, as well as dynamic ones returned from__getattr__, whereasy.__dict__only contains those objects that are attributes of theyinstance.