473,473 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Create 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__(self, attr):
return getattr(self._a, attr)

def other_methods(self):
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 1188
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__(self, attr):
return getattr(self._a, attr)

def other_methods(self):
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
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...
1
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...
4
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...
17
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
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...
12
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()...
3
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...
1
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...
1
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
1
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...
0
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...
0
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 ...

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.