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 0 1569 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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__"
|
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__
|
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__'
|
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...
|
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
| |
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):
|
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)
|
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
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |