473,383 Members | 1,880 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,383 software developers and data experts.

strange __call__

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

def b(f):
def g(*args,**kwargs):
for arg in args:
print arg
return f(*args,**kwargs)
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 1696
Rahul wrote:
Consider the following:
def a(x):
return x+1

def b(f):
def g(*args,**kwargs):
for arg in args:
print arg
return f(*args,**kwargs)
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,**kwargs):
for arg in args:
print arg
return f(*args,**kwargs)
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,**kwargs):
for arg in args:print arg
return g(*args,**kwargs)
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,**kwargs):
for arg in args:
print arg
return f(*args,**kwargs)
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,**kwargs):
for arg in args:print arg
return g(*args,**kwargs)
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,**kwargs):
for arg in args:print arg
return g(*args,**kwargs)
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__(cls, *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__(cls, *args)
...

STeVe


Jul 19 '05 #9

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

Similar topics

7
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__...
5
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...
11
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...
6
by: TK | last post by:
Hi, how can handle __call__? Can you give a sample? Thanks o-o Thomas
7
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...
5
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...
9
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...
7
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...
17
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.