Snippets

Ryan Attard Type Inspection is weird

Created by Juliette Attard last modified
class Foo(object):
    def __init__(self, **kwargs):
        self.__attr = kwargs.get('attr', None)
    @property
    def attr(self):
        return self.__attr

    @attr.setter
    def attr(self, attr):
        self.__attr = attr


class Bar(Foo):
    def __init__(self, **kwargs):
        super(Bar, self).__init__(**kwargs)


if __name__ == '__main__':
   f= Foo(attr="Hello, world")
   b= Bar(attr="Hello, world")
   print f.attr
   print b.attr
   for key in f.__class__.__dict__:
       print "f"{0}:{1}".format(key,type(f.__class__.__dict__[key]))
   for key in b.__class__.__dict__:
       print "b:{0}:{1}".format(key,type(b.__class__.__dict__[key]))
    """
rattard@rattard-T3500:~/Downloads$ python test_classes.py 
Hello, world
Hello, world
f:__module__:<type 'str'>
f:attr:<type 'property'>
f:__dict__:<type 'getset_descriptor'>
f:__weakref__:<type 'getset_descriptor'>
f:__doc__:<type 'NoneType'>
f:__init__:<type 'function'>
b:__module__:<type 'str'>
b:__doc__:<type 'NoneType'>
b:__init__:<type 'function'>
    """

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.