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 <rep...@bugs.python.org>
<https://bugs.python.org/issue31441>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to