[Tutor] python instances and type

Andreas Kostyrka andreas at kostyrka.org
Mon Apr 16 10:50:54 CEST 2007


Sorry, I cannot replicate your described behaviour:

andreas at andi-lap:~> cat /tmp/t.py
def get_class_resolution(obj):
    '''
        get the first class resolution string for a particular object

        @type obj: Mixed/ Any
        @param obj: this is the object to get the name of.  Good
                    for aiding in the comparison of two objects
                    by name and heirarchy

        @rtype: string
        @return: class name resolution of the object
    '''
    # typical output is "[<class 'class.resolution'> <type 'object'>]"
    print str(type(obj).mro())
    print str(type(obj))
    name = str(type(obj).mro()).split("'")[1]
    if len(name.split(".")) > 1:
        return ".".join(name.split(".")[:-1])
    return name

class foo_1:
   data = ""
class foo_2:
  foo_1s={}
 
x = foo_1()
x.data = "boring"
print type(x), type(x).mro()
print get_class_resolution(x)
foo_1
foo_2o = foo_2()
foo_2o.foo_1s["boring"] = x
bar = foo_2o.foo_1s.values()[0]
print type(bar), type(bar).mro()
print get_class_resolution(bar)

andreas at andi-lap:~> python2.4 /tmp/t.py
<type 'instance'> [<type 'instance'>, <type 'object'>]
[<type 'instance'>, <type 'object'>]
<type 'instance'>
instance
<type 'instance'> [<type 'instance'>, <type 'object'>]
[<type 'instance'>, <type 'object'>]
<type 'instance'>
instance
andreas at andi-lap:~> exit

Amdreas


More information about the Tutor mailing list