--- descriptor.rst 2013-05-15 18:32:52.000000000 +0200 +++ descriptor3.rst 2013-09-21 05:14:36.213309348 +0200 @@ -86,7 +86,7 @@ of ``obj``. If ``d`` defines the method :meth:`__get__`, then ``d.__get__(obj)`` is invoked according to the precedence rules listed below. -The details of invocation depend on whether ``obj`` is an object or a class. +The details of invocation depend on whether ``obj`` is a class or not. For objects, the machinery is in :meth:`object.__getattribute__` which transforms ``b.x`` into ``type(b).__dict__['x'].__get__(b, type(b))``. The @@ -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 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 + x = RevealAccess(10, 'var "x"') + y = 5 >>> m = MyClass() >>> m.x @@ -275,7 +275,7 @@ To support method calls, functions include the :meth:`__get__` method for binding methods during attribute access. This means that all functions are non-data descriptors which return bound or unbound methods depending whether -they are invoked from an object or a class. In pure python, it works like +they are invoked from a class or not. In pure python, it works like this:: class Function(object): @@ -382,7 +382,7 @@ Unlike static methods, class methods prepend the class reference to the argument list before calling the function. This format is the same -for whether the caller is an object or a class:: +for whether the caller is a class or not:: >>> class E(object): def f(klass, x):