473,786 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: proxy class and __add__ method

En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster
<ma************ @yahoo.comescri bi�:
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.Typ eError'>: 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 2535
On Jul 29, 10:23*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster *
<magnusschus... @yahoo.comescri bi :
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.Typ eError'>: 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.c om>
escribió:
On Jul 29, 10:23*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster *
<magnusschus.. .@yahoo.comescr ibi :
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.Typ eError'>: 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
1459
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 notified no matter who or what did change the wrapped object - even whenever an object internal methods changes the variables. So I coded the piece of code shown below (just copy and paste it, it's ready-to-run).
166
8679
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
1373
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 MyStr(str): """Just like ordinary strings, except it exhibits special behaviour for one particular value. """ magic = "surprise"
2
1418
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 really poor but hopefully an example will make it clear. <type 'int'> 2 1 3
10
7129
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 WebSerive contains a lot of methods and different types creating a web proxy class manually is too time consuming. I tried to edit a class manually, changing the highest-level access modifiers from public to internal - this way I could use the...
2
2126
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
5480
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 are the one which runs statically with the defining class. Hence, my understanding is that static variables must be bound to the class defining the variables and shared by children of parent class where the variable is defined. But, please have a...
2
1390
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, Shared.BO.dll I have a Windows Workflow that needs to call this, and the Workflow itself is being passed an object of this type (my.Shared.BO.InfoClass) as a property in the workflow. The same Shared.BO.dll that the web
0
994
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 ): self.__subject = subject def __getattr__( self, name ): return getattr( self.__subject, name )
0
265
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 with the Python Language Reference. I am totally clueless how to do this. Below you find my attempt. Neither __getattr__ nor __add__ of the metaclass 'meta' are ever called. May I ask you for some more guidance or corrections to the code below?
0
9647
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
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
10363
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
10164
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
10110
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
9961
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...
0
6745
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();...
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.