Recently, I find myself writing code like this:
for name in dir( object ):
if name.startswith( '__' ) : continue
...
Is there a more pythonic way to access the object’s “public” namespace?
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 prepare list of “public” attributes (as list or as generator) before:
But please read a note about
dir()function in the documentation:You should also be aware, that any class can implement
__dir__()method that may return a list of names that do not necessarily correspond to the names of the attributes. In other words,dir(something)does not guarantee that the result will return attributes ofsomething.