473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Re: proxy class and __add__ method

En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster
<ma************@yahoo.comescribi�:
Hello,
I have written the following small proxy class which I expect to pass all
function calls to the 'original' object:

--- BEGIN ---
class proxy(object):
def __init__( self, subject ):
self.__subject = subject
def __getattr__( self, name ):
return getattr( self.__subject, name )

prx_i=proxy(1)
print hasattr(prx_i,'__add__')
j=prx_i.__add__(1)
k=prx_i+1
--- END ---

Actually the "hasattr(prx_i,'__add__')" returns "True" as expected, and
"j=prx_i.__add__(1)" sets j=2.

But "k=prx_i+1" raises a
<type 'exceptions.TypeError'>: unsupported operand type(s) for +: 'proxy'
and 'int'.

How is this addition different from the previous line "j=..."? And how
can I
modify the proxy class so that all methods are passed on, which are not
explicitly overloaded?
__magic__ methods on new style classes are searched in the class, *not* in
the instance. prx_i+1 looks for __add__ in type(prx_i), that is, in the
proxy class. Try implementing a similar __getattr__ method in a metaclass.

--
Gabriel Genellina

Jul 30 '08 #1
2 2503
On Jul 29, 10:23*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster *
<magnusschus...@yahoo.comescribi :
Hello,
I have written the following small proxy class which I expect to pass all
function calls to the 'original' object:
--- BEGIN ---
class proxy(object):
* * def __init__( self, subject ):
* * * * self.__subject = subject
* * def __getattr__( self, name ):
* * * * return getattr( self.__subject, name )
prx_i=proxy(1)
print hasattr(prx_i,'__add__')
j=prx_i.__add__(1)
k=prx_i+1
--- END ---
Actually the "hasattr(prx_i,'__add__')" returns "True" as expected, and
"j=prx_i.__add__(1)" sets j=2.
But "k=prx_i+1" raises a
<type 'exceptions.TypeError'>: unsupported operand type(s) for +: 'proxy'
and 'int'.
How is this addition different from the previous line "j=..."? And how *
can I
modify the proxy class so that all methods are passed on, which are not
explicitly overloaded?

__magic__ methods on new style classes are searched in the class, *not* in *
the instance. prx_i+1 looks for __add__ in type(prx_i), that is, in the *
proxy class.
This much is true.

Try implementing a similar __getattr__ method in a metaclass.
But I don't think they use __getattr__.. they bypass it. Effectively
they catch the assignment to __add__ and cache it. You'll have to
always define it in the class and have it be ineffectual in some cases.
Jul 30 '08 #2
En Wed, 30 Jul 2008 14:54:51 -0300, Rhamphoryncus <rh****@gmail.com>
escribió:
On Jul 29, 10:23*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster *
<magnusschus...@yahoo.comescribi :
I have written the following small proxy class which I expect to pass
all
function calls to the 'original' object:
--- BEGIN ---
class proxy(object):
* * def __init__( self, subject ):
* * * * self.__subject = subject
* * def __getattr__( self, name ):
* * * * return getattr( self.__subject, name )
But "k=prx_i+1" raises a
<type 'exceptions.TypeError'>: unsupported operand type(s) for +:
'proxy'
and 'int'.
How is this addition different from the previous line "j=..."? And
how *
can I
modify the proxy class so that all methods are passed on, which are not
explicitly overloaded?

Try implementing a similar __getattr__ method in a metaclass.

But I don't think they use __getattr__.. they bypass it. Effectively
they catch the assignment to __add__ and cache it. You'll have to
always define it in the class and have it be ineffectual in some cases.
Ouch, yes, thanks, I noticed the fact after some testing.

--
Gabriel Genellina

Jul 31 '08 #3

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

Similar topics

2
by: nitrogenycs | last post by:
Hello, I need a way to get a notification whenever a variable of an object changes. The approach should be non-intrusive so that I can use existing objects without modifying them. I want to be...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
7
by: Steven D'Aprano | last post by:
I'm having problems with sub-classes of built-in types. Here is a contrived example of my subclass. It isn't supposed to be practical, useful code, but it illustrates my problem. class...
2
by: cfriedalek | last post by:
Sorry for the vague subject. Not sure what the right terminology is. How can I use an instance's data by reference to the instance name, not the instance attribute? OK the question is probably...
10
by: roberto | last post by:
I'm to deploy a .NET DLL which internally communicates with the WS. I don't want others to see internal complexity of the web service classes generated by "Add a Web reference" VS option. As a...
2
by: Bill Jackson | last post by:
For example, class A: def __init__(self,a): self.a = a def __eq__(self, other): return self.a == other.a class B: def __init__(self,b):
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
2
by: gartnerjmoody | last post by:
I have a web service that has a method that takes a created .NET class like this: method(my.Shared.BO.InfoClass info, bool isItem) The actual web service gets this class from an assembly,...
0
by: Magnus Schuster | last post by:
Hello, I have written the following small proxy class which I expect to pass all function calls to the 'original' object: --- BEGIN --- class proxy(object): def __init__( self, subject ):...
0
by: Magnus Schuster | last post by:
With this explanation the behaviour is absolutely clear. Can I find some documentation anywhere containing more background information how magic functions are resolved? I haven't been successful...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...

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.