For code:
class a(object):
a='aaa'
b=a()
print hasattr(a,'a')
print hasattr(b,'a')
who can be called by hasattr except ‘class somebody’?
Thanks!
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.
You can call
hasattrwith any object as the first argument (and any string as the second argument): it just returns False if that object does not have an attribute by that name (“having” an attribute of course includes possibly inheriting or synthesizing it;hasattr(x,'y')is True if and only if accessingx.ywould not raise an exception — that’s how it works internally: it triesgetattrand catches the exception if any).