
Untitled
By: a guest on
Jul 29th, 2012 | syntax:
None | size: 0.68 KB | hits: 12 | expires: Never
Create per-instance property descriptor?
class MyClass(object):
def __init__(self, **kwargs):
for attr, val in kwargs.items():
self.__dict__[attr] = MyDescriptor(val)
tv = MyClass(type="tv", size="30")
smartphone = MyClass(type="phone", os="android")
tv.size # do something smart with the descriptor
<property at 0x4067cf0>
class Descriptor:
def __get__(...):
# this is called when the value is got
def __set__(...
def __del__(...
obj.attr
=> type(obj).__getattribute__(obj, 'attr') is called
=> obj.__dict__['attr'] is returned if there else:
=> type(obj).__dict__['attr'] is looked up
if this contains a descriptor object then this is used.