This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author r.david.murray
Recipients Benjamin Wohlwend, docs@python, r.david.murray, rhettinger
Date 2017-09-13.13:09:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1505308153.33.0.117113013803.issue31441@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2017-09-13 13:09:13r.david.murraysetrecipients: + r.david.murray, rhettinger, docs@python, Benjamin Wohlwend
2017-09-13 13:09:13r.david.murraysetmessageid: <1505308153.33.0.117113013803.issue31441@psf.upfronthosting.co.za>
2017-09-13 13:09:13r.david.murraylinkissue31441 messages
2017-09-13 13:09:13r.david.murraycreate