Get attributes and filter out methods a private attributes
May 1, 2008 – 3:59 pmby Jake D
This articles covers how I was able to get the managed attributes from an object, and then get the public and managed attributes from an object. There used to be an __members__ method, but that has been depreciated according to http://docs.python.org/lib/specialattrs.html. Really, I hope there is a better way to do this, but I can’t seem to find it (let me know).
Quick Answer (for managed attributes only):
class dumb: def dummy(): pass def attributes(o): print "object:", o print "Initialized attribute values:" step=0 for var in dir(o): vartype=-1 exec("vartype = type(o."+str(var)+")") if not(var in o.__dict__) and not(vartype == type( dumb.dummy)) and not("__" == var[:2]): value = -30 exec("value = o."+str(dir(o)[step])) print '%40s' % var +": "+ str(value) step += 1
The whole story
Either I am just really new to python (I am) or it is just really hard when you have an object and you just want to get the public attributes.
The code above uses the dir() function to get everything from the object. Besides both public and private attributes, this also gets user defined and inherited methods. Then, i crop out those that are not public attributes.
Code Walkthrough (Get managed attributes only)
Managed attributes are those that are defined in an object’s definition. Here is an example:
class Widget(object):
"""The Widget represents an object of work. it can be processed by a worker. it is assigned to a worker by an organization."""
self.__id=id
self.test=35
### MANAGED ATTRIBUTES ###
#id
def id():
"""id is readonly"""
return self.__id
return locals()
id = property(**id())
#END id
### END MANAGED ATTRIBUTES ###
def spoil_step(self):
"""Increases the steps that this widget has been spoiling"""
old = self.__time_spoiling
self.__time_spoiling += 1
#check for sure
if old + 1 == self.__time_spoiling:
return self.__time_spoiling
else:
return False
####END Widget OBJECT ####
The Following list references the code at the top of the page
- I created a dumb object with a dummy function. This will be used to identify “instance methods”. The
passinstruction is just a filler.class dumb: def dummy(): pass - I created a function that takes an object,
o, as input. It then just prints out the object and lets the user know that it is about to go through it:def attributes(o): print "object:", o print "Initialized attribute values:" - I created a variable to keep track of which thing we are looking at, starting at the 0-index:
step=0 - Then I go through each member of the object:
for var in dir(o): - I then create a variable, and assign the type of the object to this variable:
vartype=-1 exec("vartype = type(o."+str(var)+")") - Next comes the key conditional statement:
if not(var in o.__dict__) and not(vartype == type( dumb.dummy)) and not("__" == var[:2]):The first part says exclude if you find it in
o.__dict__, which contains the private attributes from the__init__()function. The second part excludes things that are of the same type as an instance method; so this gets rid of the object’s methods. The last part looks at the first to characters of the thing, and if it is equal to__,it is excluded. - Within the conditional, we just name and value of that thing, which should be only managed attributes:
value = -30 exec("value = o."+str(dir(o)[step])) print '%40s' % var +": "+ str(value)
The next page goes over how to get both the managed and public (not just the managed) attributes from an object



