--- descriptor.rst 2010-04-13 08:43:54.000000000 +0200 +++ descriptor2.rst 2013-09-21 05:02:11.013281519 +0200 @@ -88,7 +88,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. Either way, descriptors only work for new style objects and classes. A class is new style if it is a subclass of :class:`object`. @@ -153,26 +153,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 @@ -272,7 +272,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): @@ -379,7 +379,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):