[docs] [issue31441] Descriptor example in documentation is confusing, possibly wrong

R. David Murray report at bugs.python.org
Wed Sep 13 09:09:13 EDT 2017

R. David Murray added the comment:

Here is a not-much-more-complicated version that solves the problem.  It is probably worth changing as the revised example makes clear the difference between self and obj, which is an important distinction.

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.attrname = '_' + str(random.random())[2:]                                                                                                                                                                                                                                                                                 
        self.name = name                                                                                                                                                                                                                                                                                                       
        self.initval = initval                                                                                                                                                                                                                                                                                                 
    def __get__(self, obj, objtype):                                                                                                                                                                                                                                                                                           
        print('Retrieving', self.name)                                                                                                                                                                                                                                                                                         
        return getattr(obj, self.attrname, self.initval)                                                                                                                                                                                                                                                                       
    def __set__(self, obj, val):                                                                                                                                                                                                                                                                                               
        print('Updating', self.name)                                                                                                                                                                                                                                                                                           
        setattr(obj, self.attrname, val)                                                                                                                                                                                                                                                                                       
class MyClass:                                                                                                                                                                                                                                                                                                                 
    x = RevealAccess(10, 'var "x"')                                                                                                                                                                                                                                                                                            
    y = 5

nosy: +r.david.murray, rhettinger

Python tracker <report at bugs.python.org>

More information about the docs mailing list