473,545 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

decorating container types (Python 2.4)

Hi,

I have a container class A and I want to add functionality to it by
using a decorator class B, as follows:

class A(object):
def __len__(self):
return 5

class B(object):
def __init__(self, a):
self._a = a

def __getattr__(sel f, attr):
return getattr(self._a , attr)

def other_methods(s elf):
blah blah blah

I was expecting len(B) to return 5 but I get
AttributeError: type object 'B' has no attribute '__len__'
instead.
I was expecting len() to call B.__len__() which would invoke
B.__getattr__ to call A.__len__ but __getattr__ is not being called.
I can work around this, but I am curious if anyone knows _why_
__getattr__ is not being called in this situation.

Thanks
Tim

Oct 11 '07 #1
1 1190
On Oct 11, 5:42 pm, timar...@gmail. com wrote:
Hi,

I have a container class A and I want to add functionality to it by
using a decorator class B, as follows:

class A(object):
def __len__(self):
return 5

class B(object):
def __init__(self, a):
self._a = a

def __getattr__(sel f, attr):
return getattr(self._a , attr)

def other_methods(s elf):
blah blah blah

I was expecting len(B) to return 5 but I get
AttributeError: type object 'B' has no attribute '__len__'
instead.
I was expecting len() to call B.__len__() which would invoke
B.__getattr__ to call A.__len__ but __getattr__ is not being called.
I can work around this, but I am curious if anyone knows _why_
__getattr__ is not being called in this situation.

Thanks
Tim
Unfortunately __getattr__ is not called for special attributes; I'm
not sure if this is by design or a technical limitation. You have to
manually delegate all special methods (or perhaps write a metaclass
that does this for you).

George

Oct 12 '07 #2

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

Similar topics

7
1681
by: Kamilche | last post by:
I want to convert a dict into string form, then back again. After discovering that eval is insecure, I wrote some code to roll a Python object, dict, tuple, or list into a string. I've posted it below. Does anyone know an easier way to accomplish this? Essentially, I want to avoid doing an 'eval' on a string to get it back into dict form......
1
1681
by: Tim Conkling | last post by:
I've run into a problem with a game I'm designing -- I (think) I need to do something ugly, for speed reasons, and I'm wondering if people here might have any ideas about how solve my problem more elegantly: I'm using a templated container that I created for this project that sorts objects based on a numerical value. That is, each object in...
4
2757
by: m | last post by:
Hello I need to use sth. like container for different types of variables. Is sth like this in c++ STL? ----|----------|--|----|------|etc. var1| var2 |v3|var4| var5 |etc. int | float |bo|int | char |etc.
17
2568
by: tuvok | last post by:
How can objects of different types be stored in a collection? For example: struct S1 { /*...*/ }; struct S2 { /*...*/ }; struct Sn { /*...*/ }; template<typename T> class X { public:
6
6512
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying "yes", but i vaguely remember having read that it's not exactly like that one might say that "name mangling" is a part of c++ compiler specification...
12
4070
by: Andy Terrel | last post by:
Okay does anyone know how to decorate class member functions? The following code gives me an error: Traceback (most recent call last): File "decorators2.py", line 33, in <module> s.update() File "decorators2.py", line 13, in __call__ retval = self.fn.__call__(*args,**kws) TypeError: update() takes exactly 1 argument (0 given)
3
2291
by: Alexander Draeger | last post by:
Hello everybody, I'm very interesting in using the decorator concept, but I can't convert it in useful things. I have read many about decorators and have seen a lot of examples, but I search a possibility, to decorate methods of classes with reference to the instances. For example: I have a class A: class A(object):
1
2129
by: hardieca | last post by:
Hi! I decorate my unfinished classes and methods with a custom TODO attribute (as in things To Do). Using reflection, I am then able to parse through my classes and output all TODOs to a list I can examine to figure out what is left to be done in my application. The attribute is coded thusly: AttributeUsage((AttributeTargets.Class |...
1
1667
by: 1x7y2z9 | last post by:
Say, we have a (parent) class P. It has N child classes C1(P), C2(P) ... CN(P) Each of the child classes defines (differently) a method func(). I wish to decorate all of the CX.func() in the same way. One way to do this is to add a decorator to each of the derived classes. But this is tedious and involves modifying multiple files. Is...
0
7464
marktang
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...
0
7396
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...
0
7656
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. ...
0
7751
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...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4943
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...
0
3449
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...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
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 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.