Get attributes and filter out methods a private attributes

May 1, 2008 – 3:59 pm
by Jake D

Get Managed attributes, and public attributes

Python uses some convention for denoting a public attribute vs a private attribute.  The append the string “_[object name]” to the front of the variable.  To indicate that you want a variable to be private, you put two leading underscores (and no more than one trailing).

Also, if you try to print something like:

print str(o.__class__)

You get out put like:


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)+")")
        class_name_length = len(str(o.__class__)) - 19
        compare1 = "<class '__main__."+var[1:class_name_length + 1]+"'>"
        compare2 = str(o.__class__)
        #print compare1
        print compare2
        if not(compare1 == compare2) 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

On the next page, I include a full source for a function that will take an object and give a summary of each attributes read, write, and delete.

Pages: 1 2 3 4



Rate this article:


(No Ratings Yet)
Loading ... Loading ...

Advertisement:




Post a Comment