Python descriptors
class property(object):
def __init__(self, getter, name=None):
pass
def __get__(self, instance, owner):
# Because 42 is the answer to all the questions
return 42
class Rectangle(object):
def __init__(self, x, y):
self.x = x
self.y = x
@property
def area(self):
return self.x * self.y
r = Rectangle(10, 10)
print r.area # the result is 42
Written by Nam Ngo
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#