--- descriptor.rst 2013-05-15 18:32:52.000000000 +0200 +++ descriptor3.rst 2013-09-19 19:30:30.592117953 +0200 @@ -145,26 +145,26 @@ alternate approach that could do this for every attribute. However, this descriptor is useful for monitoring just a few chosen attributes:: - class RevealAccess(object): - """A data descriptor that sets and returns values - normally and prints a message logging their access. - """ - - def __init__(self, initval=None, name='var'): - self.val = initval - self.name = name - - def __get__(self, obj, objtype): - print('Retrieving', self.name) - return self.val - - def __set__(self, obj, val): - print('Updating', self.name) - self.val = val - - >>> class MyClass(object): - x = RevealAccess(10, 'var "x"') - y = 5 + >>> class RevealAccess: + """A data descriptor that sets and returns values + normally and prints a message logging their access. + """ + + def __init__(self, initval=None, name='var'): + self.val = initval + self.name = name + + def __get__(self, obj, objtype): + print('Retrieving', self.name) + return self.val + + def __set__(self, obj, val): + print('Updating', self.name) + self.val = val + + >>> class MyClass: + x = RevealAccess(10, 'var "x"') + y = 5 >>> m = MyClass() >>> m.x @@ -194,7 +194,7 @@ The documentation shows a typical use to define a managed attribute ``x``:: - class C(object): + class C: def getx(self): return self.__x def setx(self, value): self.__x = value def delx(self): del self.__x @@ -203,7 +203,7 @@ To see how :func:`property` is implemented in terms of the descriptor protocol, here is a pure Python equivalent:: - class Property(object): + class Property: "Emulate PyProperty_Type() in Objects/descrobject.c" def __init__(self, fget=None, fset=None, fdel=None, doc=None): @@ -250,7 +250,7 @@ affect existing client code accessing the attribute directly. The solution is to wrap access to the value attribute in a property data descriptor:: - class Cell(object): + class Cell: . . . def getvalue(self, obj): "Recalculate cell before returning value" @@ -278,7 +278,7 @@ they are invoked from an object or a class. In pure python, it works like this:: - class Function(object): + class Function: . . . def __get__(self, obj, objtype=None): "Simulate func_descr_get() in Objects/funcobject.c" @@ -286,7 +286,7 @@ Running the interpreter shows how the function descriptor works in practice:: - >>> class D(object): + >>> class D: def f(self, x): return x @@ -358,7 +358,7 @@ Since staticmethods return the underlying function with no changes, the example calls are unexciting:: - >>> class E(object): + >>> class E: def f(x): print(x) f = staticmethod(f) @@ -371,7 +371,7 @@ Using the non-data descriptor protocol, a pure Python version of :func:`staticmethod` would look like this:: - class StaticMethod(object): + class StaticMethod: "Emulate PyStaticMethod_Type() in Objects/funcobject.c" def __init__(self, f): @@ -384,7 +384,7 @@ argument list before calling the function. This format is the same for whether the caller is an object or a class:: - >>> class E(object): + >>> class E: def f(klass, x): return klass.__name__, x f = classmethod(f) @@ -401,7 +401,7 @@ :func:`dict.fromkeys` creates a new dictionary from a list of keys. The pure Python equivalent is:: - class Dict(object): + class Dict: . . . def fromkeys(klass, iterable, value=None): "Emulate dict_fromkeys() in Objects/dictobject.c" @@ -419,7 +419,7 @@ Using the non-data descriptor protocol, a pure Python version of :func:`classmethod` would look like this:: - class ClassMethod(object): + class ClassMethod: "Emulate PyClassMethod_Type() in Objects/funcobject.c" def __init__(self, f):