Snippets

Ryan Attard Type Inspection is weird

You are viewing an old version of this snippet. View the current version.
Revised by Juliette Attard b0a46b7
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 "{0}:{1}".format(key,type(f.__class__.__dict__[key]))
   for key in b.__class__.__dict__:
       print "{0}:{1}".format(key,type(b.__class__.__dict__[key]))
    """
rattard@rattard-T3500:~/Downloads$ python test_classes.py 
Hello, world
Hello, world
__module__:<type 'str'>
attr:<type 'property'>
__dict__:<type 'getset_descriptor'>
__weakref__:<type 'getset_descriptor'>
__doc__:<type 'NoneType'>
__init__:<type 'function'>
__module__:<type 'str'>
__doc__:<type 'NoneType'>
__init__:<type 'function'>
    """
HTTPS SSH

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