473,573 Members | 3,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange __call__

Consider the following:
def a(x):
return x+1

def b(f):
def g(*args,**kwarg s):
for arg in args:
print arg
return f(*args,**kwarg s)
return g

a.__call__ = b(a.__call__)

now calling a(1) and a.__call__(1) yield 2 different results!!
i.e. for functions a(1) doesnt seem to be translated to a.__call__ if
you assign a new value to a.__call__?
i am using python 2.3.3
somebody please clear this confusion

Jul 19 '05 #1
8 1706
Rahul wrote:
Consider the following:
def a(x):
return x+1

def b(f):
def g(*args,**kwarg s):
for arg in args:
print arg
return f(*args,**kwarg s)
return g

a.__call__ = b(a.__call__)

now calling a(1) and a.__call__(1) yield 2 different results!!
i.e. for functions a(1) doesnt seem to be translated to a.__call__ if
you assign a new value to a.__call__?


I don't know why this happens, but setting the __call__ attribute of a
is a pretty strange thing to do. Why not just set a instead? The
original function a(x) will still be stored as a closure in what is
returned from b().

If this is of purely academic interest then the answer is I don't know. :)
--
Michael Hoffman
Jul 19 '05 #2
Just a guess, but setting "__X__" special methods won't work in most cases
because these are usually optimized when the class is created.

It might work if a.__call__ did exist before (because class a: contained
a __call__ definition).

Andreas

On Wed, Jun 29, 2005 at 09:15:45AM +0100, Michael Hoffman wrote:
Rahul wrote:
Consider the following:
def a(x):
return x+1

def b(f):
def g(*args,**kwarg s):
for arg in args:
print arg
return f(*args,**kwarg s)
return g

a.__call__ = b(a.__call__)

now calling a(1) and a.__call__(1) yield 2 different results!!
i.e. for functions a(1) doesnt seem to be translated to a.__call__ if
you assign a new value to a.__call__?


I don't know why this happens, but setting the __call__ attribute of a
is a pretty strange thing to do. Why not just set a instead? The
original function a(x) will still be stored as a closure in what is
returned from b().

If this is of purely academic interest then the answer is I don't know. :)
--
Michael Hoffman
--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #3
Hi.
well if you do dir(a) just after defining 'a' then it does show
'__call__'.
the reason i wanted to do it is that i wanted to see if theres a
uniform way to wrap a function and callable objects so that for
example i can get some message printed whenever a function or a
function-like-object is called. then i could simply do :

def wrapper(obj):
g = obj.__call__
def f(*args,**kwarg s):
for arg in args:print arg
return g(*args,**kwarg s)
obj.__call__=f
but it seems this will not work for functions :(
Andreas Kostyrka wrote:
Just a guess, but setting "__X__" special methods won't work in most cases
because these are usually optimized when the class is created.

It might work if a.__call__ did exist before (because class a: contained
a __call__ definition).

Andreas

On Wed, Jun 29, 2005 at 09:15:45AM +0100, Michael Hoffman wrote:
Rahul wrote:
Consider the following:
def a(x):
return x+1

def b(f):
def g(*args,**kwarg s):
for arg in args:
print arg
return f(*args,**kwarg s)
return g

a.__call__ = b(a.__call__)

now calling a(1) and a.__call__(1) yield 2 different results!!
i.e. for functions a(1) doesnt seem to be translated to a.__call__ if
you assign a new value to a.__call__?


I don't know why this happens, but setting the __call__ attribute of a
is a pretty strange thing to do. Why not just set a instead? The
original function a(x) will still be stored as a closure in what is
returned from b().

If this is of purely academic interest then the answer is I don't know. :)
--
Michael Hoffman
--
http://mail.python.org/mailman/listinfo/python-list


Jul 19 '05 #4
Rahul wrote:
def wrapper(obj):
g = obj.__call__
def f(*args,**kwarg s):
for arg in args:print arg
return g(*args,**kwarg s)
obj.__call__=f
but it seems this will not work for functions :(


def wrap(obj):
def f(*args, **kwargs):
for arg in args:
print arg
return obj(*args, **kwargs)
return f

@wrap
def func(a, b, c):
...

class C(object):
...
C = wrap(C)

STeVe
Jul 19 '05 #5
If you do C = wrap(C) C no longer remains a class..it becomes a
function.

Steven Bethard wrote:
Rahul wrote:
def wrapper(obj):
g = obj.__call__
def f(*args,**kwarg s):
for arg in args:print arg
return g(*args,**kwarg s)
obj.__call__=f
but it seems this will not work for functions :(


def wrap(obj):
def f(*args, **kwargs):
for arg in args:
print arg
return obj(*args, **kwargs)
return f

@wrap
def func(a, b, c):
...

class C(object):
...
C = wrap(C)

STeVe


Jul 19 '05 #6
Rahul wrote:
If you do C = wrap(C) C no longer remains a class..it becomes a
function.


Does that matter?

Reinhold
Jul 19 '05 #7
Steven Bethard wrote:

def wrap(obj):
def f(*args, **kwargs):
for arg in args:
print arg
return obj(*args, **kwargs)
return f

@wrap
def func(a, b, c):
...

class C(object):
...
C = wrap(C)
Rahul top-posted: If you do C = wrap(C) C no longer remains a class..it becomes a
function.


And if you do
func = wrap(func)
which is the equivalent of
@wrap
def func(...):
...
then func no longer has the same signature. But as Reinhold suggests,
does that really matter? In the case of the class, you can still call
it to create class instances. In the case of the function, you can
still call it to retrieve return values. Why do you care about the type
of the object?

In the case that it does matter, e.g. you want to be able to invoke your
methods from the class instead of the instance, you can wrap the
specific function that you need wrapped, e.g.

class C(object):
@wrap
def __new__(cls, *args):
super(C, cls).__new__(cl s, *args)
...

STeVe
Jul 19 '05 #8
Hi.
I understood your point.
thanks...
rahul
Steven Bethard wrote:
Steven Bethard wrote:

def wrap(obj):
def f(*args, **kwargs):
for arg in args:
print arg
return obj(*args, **kwargs)
return f

@wrap
def func(a, b, c):
...

class C(object):
...
C = wrap(C)


Rahul top-posted:
> If you do C = wrap(C) C no longer remains a class..it becomes a
> function.


And if you do
func = wrap(func)
which is the equivalent of
@wrap
def func(...):
...
then func no longer has the same signature. But as Reinhold suggests,
does that really matter? In the case of the class, you can still call
it to create class instances. In the case of the function, you can
still call it to retrieve return values. Why do you care about the type
of the object?

In the case that it does matter, e.g. you want to be able to invoke your
methods from the class instead of the instance, you can wrap the
specific function that you need wrapped, e.g.

class C(object):
@wrap
def __new__(cls, *args):
super(C, cls).__new__(cl s, *args)
...

STeVe


Jul 19 '05 #9

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

Similar topics

7
2492
by: Patrick Lioi | last post by:
def foo(): pass foo is a function foo is a callable object foo has method __call__ defined foo.__call__ is a function foo.__call__ is a callable object foo.__call__ has method __call__ defined
5
2360
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still confused. I'd like to have a factory class that takes a string argument and returns the appropriate factory method based on that string. I'd like...
11
1863
by: Stefan Behnel | last post by:
Hi! This somewhat puzzles me: Python 2.4 (#1, Feb 3 2005, 16:47:05) on linux2 Type "help", "copyright", "credits" or "license" for more information. ..>>> class test(object): .... def __init__(self):
6
2686
by: TK | last post by:
Hi, how can handle __call__? Can you give a sample? Thanks o-o Thomas
7
1767
by: ncf | last post by:
I have a feeling that this is highly unlikely, but does anyone in here know if it's possible to directly call a module, or will I have to wrap it up in a class? i.e., import MyMod MyMod.whatever = "Hi?" MyMod("meow mix")
5
1355
by: Kent Johnson | last post by:
I am learning about metaclasses and there is something that confuses me. I understand that if I define a __call__ method for a class, then instances of the class become callable using function syntax: >>> class Foo(object): ... def __call__(self): ... print 'Called Foo' ... >>> f=Foo() >>> f()
9
1650
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class M_A (type) :
7
2168
by: Gigs_ | last post by:
from Tkinter import * from tkFileDialog import askopenfilename from tkColorChooser import askcolor from tkMessageBox import askquestion, showerror from tkSimpleDialog import askfloat demos = { 'Open': askopenfilename, 'Color': askcolor, 'Query': lambda: askquestion('Warning', 'You typed
17
1663
by: skip | last post by:
I don't personally use __call__ methods in my classes, but I have encountered it every now and then here at work in code written by other people. The other day I replaced __call__ with a more obvious method name, so now instead of executing obj(foo, bar, baz) the code in question is obj.execute(foo, bar, baz)
0
7776
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
7686
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
8014
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
8060
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...
1
5581
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5289
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
3730
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
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2197
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.