python - Accessing an object's attribute inside __setattr__ -


python bit me today. i'm trying access object's attribute inside __setattr__ implementation - can't figure out how. i've tried far:

class test1(object):     def __init__(self):         self.blub = 'hi1'      def __setattr__(self, name, value):         print self.blub  class test2(object):     def __init__(self):         self.blub = 'hi2'      def __setattr__(self, name, value):         print object.__getattr__(self, 'blub')  class test3(object):     def __init__(self):         self.blub = 'hi3'      def __setattr__(self, name, value):         print object.__getattribute__(self, 'blub')  class test4(object):     def __init__(self):         self.blub = 'hi4'      def __setattr__(self, name, value):         print self.__getattr__('blub')  class test5(object):     def __init__(self):         self.blub = 'hi5'      def __setattr__(self, name, value):         print self.__getattribute__('blub')  class test6(object):     def __init__(self):         self.blub = 'hi6'      def __setattr__(self, name, value):         print self.__dict__['blub'] 

testing:

try:     testx().bip = 'bap' except exception ex:     print ex 

with x 1 6

output:

'test1' object has no attribute 'blub' type object 'object' has no attribute '__getattr__' 'test3' object has no attribute 'blub' 'test4' object has no attribute '__getattr__' 'test5' object has no attribute 'blub' 'blub' 

any suggestions?

op, haven't told whole story. did not run code this:

testx().bip = 'bap' 

you ran code this:

try:     testx().bip = 'bap' except exception ex:     print ex 

there's big difference. why, ask? well, output seems on first glance indicate test6 works, , several comments , answers assumed did. why appear work? reading code, there's no way should work. closer inspection of source code reveals if had worked, should have printed hi6, not 'blub'.

i put breakpoint @ print ex line in pdb examine exception:

(pdb) ex keyerror('blub',) (pdb) print ex 'blub' 

for reason print ex not print keyerror: blub you'd expect, 'blub', why test6 appeared work.

so we've cleared up. in future, please not leave out code because might important.

all other answers correctly point out have not set attribute you're attempting print, , problem. answer had accepted previously, before accepted answer istead, prescribed following solution:

def __setattr__(self, name, value):     self.__dict__[name] = value     print self.__dict__[name] 

while solution indeed work, not design. because might want change base class @ point, , base class might might have important side effects when setting and/or getting attributes, or might not store attributes in self.__dict__ @ all! better design avoid messing around __dict__.

the correct solution invoke parent __setattr__ method, , suggested @ least 1 other answer, though wrong syntax python 2.x. here's how i'd it:

def __setattr__(self, name, value):     super(test6, self).__setattr__(name, value)     print getattr(self, name) 

as see i'm using getattr instead of __dict__ attributes dynamically. unlike using __dict__ directly, call self.__getattr__ or self.__getattribute__, appropriate.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -