Skip to content

Instantly share code, notes, and snippets.

@fcamel
Created February 15, 2011 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcamel/827637 to your computer and use it in GitHub Desktop.
Save fcamel/827637 to your computer and use it in GitHub Desktop.
attribute and descriptor
class X(object):
property = property(fget=lambda x: "property value")
direct_attribute = "direct_attribute"
def method(self):
return "method value: %s" % id(self)
@classmethod
def classmethod(self):
return "classmethod value: %s" % id(self)
@staticmethod
def staticmethod():
return "staticmethod value"
x = X()
print 'x.__dict__'
print x.__dict__
print '\nx.property'
print '-' * 60
print "x.property ", x.property
print "type(x).__dict__['property'] ", type(x).__dict__['property']
print "type(x).__dict__['property'].__get__(x, type(x))", type(x).__dict__['property'].__get__(x, type(x))
print '\nx.direct_attribute'
print '-' * 60
print "x.direct_attribute ", x.direct_attribute
print "type(x.direct_attribute) ", type(x.direct_attribute)
print "type(x).__dict__['direct_attribute']", type(x).__dict__['direct_attribute']
print '\nx.method'
print '-' * 60
print "id(x) ", id(x)
print "x.method() ", x.method()
print "x.method ", x.method
print "type(x).__dict__['method'] ", type(x).__dict__['method']
print "type(x).__dict__['method'].__get__(x, type(x))", type(x).__dict__['method'].__get__(x, type(x))
print '\nx.classmethod'
print '-' * 60
print "id(type(x)) ", id(type(x))
print "x.classmethod() ", x.classmethod()
print "x.classmethod ", x.classmethod
print "type(x).__dict__['classmethod'] ", type(x).__dict__['classmethod']
print "type(x).__dict__['classmethod'].__get__(x, type(x))", type(x).__dict__['classmethod'].__get__(x, type(x))
print '\nx.staticmethod'
print '-' * 60
print "x.staticmethod() ", x.staticmethod()
print "x.staticmethod ", x.staticmethod
print "type(x).__dict__['staticmethod'] ", type(x).__dict__['staticmethod']
print "type(x).__dict__['staticmethod'].__get__(x, type(x))", type(x).__dict__['staticmethod'].__get__(x, type(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment