Snippets

Ryan Attard Type Inspection is weird

Updated by Juliette Attard

File test_classes.py Modified

  • Ignore whitespace
  • Hide word diff
    print f.attr
    print b.attr
    for key in f.__class__.__dict__:
-       print "{0}:{1}".format(key,type(f.__class__.__dict__[key]))
+       print "f"{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]))
+       print "b:{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'>
+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'>
     """
Updated by Juliette Attard

File test_classes.py Modified

  • Ignore whitespace
  • Hide word diff
 
 
 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 type(b.__class__.__dict__[key])
-
-"""
-Returns:
+       print "{0}:{1}".format(key,type(b.__class__.__dict__[key]))
+    """
 rattard@rattard-T3500:~/Downloads$ python test_classes.py 
 Hello, world
-<type 'str'>
-<type 'NoneType'>
-<type 'function'>
-"""
+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'>
+    """
Created by Juliette Attard

File test_classes.py Added

  • Ignore whitespace
  • Hide word diff
+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__':
+   b= Bar(attr="Hello, world")
+   print b.attr
+   for key in b.__class__.__dict__:
+       print type(b.__class__.__dict__[key])
+
+"""
+Returns:
+rattard@rattard-T3500:~/Downloads$ python test_classes.py 
+Hello, world
+<type 'str'>
+<type 'NoneType'>
+<type 'function'>
+"""
HTTPS SSH

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