class EventSource(object):
def __init__(self):
self._callbacks = []
def register(self, callback):
print('register:', callback)
self._callbacks.append(callback)
def notify(self, *args, **kwargs):
for callback in self._callbacks:
callback(*args, **kwargs)
class EventHandler(object):
def __init__(self, name):
self.name = name
def foo(self, *args, **kwargs):
print('foo:', self …
-
-
Item 27: classmethod
class A(object): value = 5 @classmethod def cls_fun(cls): print('A.cls_fun', cls, cls.value) def fun(self): print('A.fun', self, self.value) self.cls_fun() class B(A): value = 10 class C(A): value = 15 @classmethod def cls_fun(cls): print('B.cls_fun', cls, cls.value) def fun2(self): print …
-
Item 26: staticmethod
class A(object): @staticmethod def static_fun(): print('A.static_fun') def fun(self): print('A.fun', self) print('#', end='') self.static_fun() class B(A): @staticmethod def static_fun(): print('B.static_fun') def fun2(self): print('B.fun2', self) print('#', end='') self.static_fun() a = A(); a.static_fun(); a.fun() print('----') b = B(); b …
-
Item 25: Metaclass
class Field(object): def __get__(self, instance, owner): print('__get__', self, instance, self.name) return getattr(instance, self.name) def __set__(self, instance, value): print('__set__', self, instance, self.name, value) setattr(instance, self.name, value) class Meta(type): def __new__(meta, name, bases, class_dict): print('__new__', name, bases, class_dict …
-
Item 24: List
k = 3 x = [0] * k y = [[0] * k] * k z = [[0] * k for i in range(k)] print(x, y, z) x[0] = 1 x[1] = 2 y[0][0] = 1 y[0][1] = 2 z[0][0] = 1 z[0][1] = 2 print(x, y, z)
-
Item 23: getattribute
class A(object): def __init__(self): self.x = 5 def __getattr__(self, name): print(' A.__getattr__', self, name) if name and name[0].isalpha(): setattr(self, name, 10) return 10 raise AttributeError('cannot get ' + name) class B(A): def __getattribute__(self, name): print(' B.__getattribute__', self, name) if name == 'z …
-
Item 22: getattr and setattr
class A(object): def __init__(self): self.x = 5 def __getattr__(self, name): print('A.__getattr__(', self, ',', name, ')') setattr(self, name, 10) return 10 def __setattr__(self, name, value): print('A.__setattr__(', self, ',', name, ',', value, ')') self.__dict__[name] = value a = A() print('P1:', a.x) print('P2:', a.x) a …
-
Item 21: Descriptors
class Field(object): def __init__(self, name): self.name = '__' + name def __get__(self, instance, owner): print('__get__', self, instance, owner) return getattr(instance, self.name) def __set__(self, instance, value): print('__set__', self, instance, value) setattr(instance, self.name, value) class Record(object): a = Field('a') b = Field('b …
-
Item 20: for Loop Variable
a = 0 for x in range(3): a += 1 b = 0 for y in range(5): for z in range(8): b += 1 c = 0 for w in range(13): for w in range(23): c += 1 print(x, y, z, w) print(a, b, c)
-
Item 19: Class Property
class Temperature(object): def __init__(self, celsius): self.celsius = celsius def get_fahrenheit(self): return self.celsius / 5 * 9 + 32 def set_fahrenheit(self, fahrenheit): self.celsius = (fahrenheit - 32) / 9 * 5 fahrenheit = property(get_fahrenheit, set_fahrenheit) t = Temperature(30) print(t.celsius, t.fahrenheit) t.fahrenheit = 50 print(t.celsius, t.fahrenheit)