473,729 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__setattr__ and __getattr__ with derived classes

class base:
def __setattr__(sel f,attr,key,*une xpected):
print "Base Class :",attr,key,une xpected,self.__ dict__
self.__dict__[attr] = key
def __getattr__(sel f,attr,*unexpec ted):
print "Base Class :",attr,unexpec ted,self.__dict __
return self.__dict__[attr]

class derived(base):
def __setattr__(sel f,attr,key,*une xpected):
print "Derived Class :",attr,key,une xpected,self.__ dict__
self.__dict__[attr] = key
## def __getattr__(sel f,attr,*unexpec ted):
## print "Derived Class :",attr,unexpec ted
## return self.__dict__[attr]
if __name__ == '__main__':
c = derived()
d = base()
print 'No Init yet'
def init(self):
print 'inside init'
self.test = None
self.test1 = 'string'
self.test2 = 2
print self.test
print self.test1
print self.test2
def init2(self):
print 'inside init 2'
self.test = None
self.test3 = 'string'
self.test4 = 2
print self.test
## print self.test1
## print self.test2
base.__dict__['__init__'] = init
c = derived()
d = base()
c.test
d.test
print "Base Had init"
derived.__dict_ _['__init__'] = init2
c = derived()
d = base()
print c.test1
print d.test1
=============== =============== =============== ==========
Output
+++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++
No Init yet
inside init
Derived Class : test None () {}
Derived Class : test1 string () {'test': None}
Derived Class : test2 2 () {'test': None, 'test1': 'string'}
None
string
2
inside init
Base Class : test None () {}
Base Class : test1 string () {'test': None}
Base Class : test2 2 () {'test': None, 'test1': 'string'}
None
string
2
Base Had init
inside init 2
Derived Class : test None () {}
Derived Class : test3 string () {'test': None}
Derived Class : test4 2 () {'test': None, 'test3': 'string'}
None
inside init
Base Class : test None () {}
Base Class : test1 string () {'test': None}
Base Class : test2 2 () {'test': None, 'test1': 'string'}
None
string
2
Base Class : test1 () {'test': None, 'test3': 'string', 'test4': 2}
=============== =============== =============== =============== =========
look at the self.__dict__ in __init__ method
Base Class : test2 2 () {'test': None, 'test1': 'string'}

Now how did it change to
Base Class : test1 () {'test': None, 'test3': 'string', 'test4': 2}
in __getattr_ method?
I get a key error after this point. the dictionary contents in base
class were just fine in the __init__ method. but when i call the
__getattr__ python is giving me a different dictionary!!!

Can someone explain what is happening?
i am using python 2.3.3
Jul 18 '05 #1
0 1573

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1415
by: Holger Joukl | last post by:
Hi there, please excuse my rather lengthy post. With introduction of the new style classes, something seems to have changed for __getattr__ hooks, even for classic classes: getattr.py: class A: # classic! def __getattr__(self, attr): print "-->A.__getattr__"
1
3574
by: limodou | last post by:
I found it puzzled me that: class A: def __getattr__(self, name): return None a=A() b=A() a==b will raise Exception: __eq__
3
4397
by: Thomas Heller | last post by:
Just wondering about this behaviour, why is it this way? Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> object.__setattr__ <slot wrapper '__setattr__' of 'object' objects> >>> object.__getattr__ Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: type object 'object' has no attribute '__getattr__'
3
1604
by: L.C. Rees | last post by:
Can custom __setattr__ methods and properties be mixed in new style classes? I experimented with a new style class with both a custom __setattr__ method and a property. Attributes controlled by the __setattr__ method work fine. When I attempt to set or get the property it raises the error: "TypeError: _settext() takes exactly two arguments (1 given)". This suggests that the __setattr__ may be conflicting with assignment to the property...
1
3790
by: aum | last post by:
Hi, Does Javascript have any equivalent to Python's __getattr__ and __setattr__ methods? In other words, the option to define a method of a class that gets invoked whenever someone tries to fetch an unknown attribute, or set any attribute of an instance of that class? I've looked at __defineGetter__, __defineSetter__, __lookupGetter__ and
4
3941
by: Enrico | last post by:
Hi there, I have the following situation (I tryed to minimize the code to concentrate on the issue): def __getattr__(self, name): print 'A.__getattr__' if name == 'a': return 1 raise AttributeError('%s not found in A' % name) def __getattr__(self, name):
2
2304
by: mwojc | last post by:
Hi! My class with implemented __getattr__ and __setattr__ methods cannot be pickled because of the Error: ====================================================================== ERROR: testPickle (__main__.TestDeffnet2WithBiases) ---------------------------------------------------------------------- Traceback (most recent call last): File "deffnet.py", line 246, in testPickle cPickle.dump(self.denet, file)
7
1709
by: =?UTF-8?Q?Alexandru_Mo=C8=99oi?= | last post by:
i'm facing the following problem: class Base(object): def __getattr__(self, attr): return lambda x: attr + '_' + x def dec(callable): return lambda *args: 'dec_' + callable(*args) class Derived(Base): what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have
2
1425
by: Jan Schilleman | last post by:
Hi all, I am trying to redefine __setattr__. The base class is in a library (actually, it is win32com.client.DispatchBaseClass) and I do not want to touch it. My problem is exemplified below. To my surprise, __setattr__ and __str__ behave differently; I can redefine __str__ and the inherited __str__ is still the redefined one. But redefining __setattr__ on the base class does not get inherited. In Base.__dict__ the __setattr__ is the...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.