From: GZ on
I want to store a reference to a function into a class property.

So I am expecting that:

class A:
fn = lambda x: x

fn = A.fn
fn(1)

Traceback (most recent call last):
File "<string>", line 1, in <string>
TypeError: unbound method <lambda>() must be called with A instance as
first argument (got int instance instead)


The problem is that A.fn is treated as a bounded method. I really want
A.fn to be a variable that stores a reference to a function. Is there
any way to achieve this?

Thanks,
GZ
From: Chris Rebert on
On Tue, Apr 27, 2010 at 4:36 PM, GZ <zyzhu2000(a)gmail.com> wrote:
> I want to store a reference to a function into a class property.
>
> So I am expecting that:
>
> class A:
>     fn = lambda x: x
>
> fn = A.fn
> fn(1)
>
> Traceback (most recent call last):
>  File "<string>", line 1, in <string>
> TypeError: unbound method <lambda>() must be called with A instance as
> first argument (got int instance instead)
>
>
> The problem is that A.fn is treated as a bounded method. I really want
> A.fn to be a variable that stores a reference to a function. Is there
> any way to achieve this?

Use the staticmethod() decorator:

class A(object):
@staticmethod
def fn(x):
return x

#rest same as before

Cheers,
Chris
--
http://blog.rebertia.com
From: Terry Reedy on
On 4/27/2010 7:36 PM, GZ wrote:
> I want to store a reference to a function into a class property.
>
> So I am expecting that:
>
> class A:
> fn = lambda x: x
>
> fn = A.fn
> fn(1)
>
> Traceback (most recent call last):
> File "<string>", line 1, in<string>
> TypeError: unbound method<lambda>() must be called with A instance as
> first argument (got int instance instead)
>
>
> The problem is that A.fn is treated as a bounded method. I really want
> A.fn to be a variable that stores a reference to a function. Is there
> any way to achieve this?

Use 3.1, though you will have the same issue when calling it and same
fix that Chris gave.

Terry Jan Reedy



From: GZ on
Hi Chris,

On Apr 27, 6:43 pm, Chris Rebert <c...(a)rebertia.com> wrote:
> On Tue, Apr 27, 2010 at 4:36 PM, GZ <zyzhu2...(a)gmail.com> wrote:
> > I want to store a reference to a function into a class property.
>
> > So I am expecting that:
>
> > class A:
> >     fn = lambda x: x
>
> > fn = A.fn
> > fn(1)
>
> > Traceback (most recent call last):
> >  File "<string>", line 1, in <string>
> > TypeError: unbound method <lambda>() must be called with A instance as
> > first argument (got int instance instead)
>
> > The problem is that A.fn is treated as a bounded method. I really want
> > A.fn to be a variable that stores a reference to a function. Is there
> > any way to achieve this?
>
> Use the staticmethod() decorator:
>
> class A(object):
>     @staticmethod
>     def fn(x):
>         return x
>
> #rest same as before
>
> Cheers,
> Chris
> --http://blog.rebertia.com- Hide quoted text -
>
> - Show quoted text -

I do not think it will help me. I am not trying to define a function
fn() in the class, but rather I want to make it a "function reference"
so that I can initialize it any way I like later.

For example, I want to be able to write the following:

A.fn = lambda x : x*x
f = A.fn
f(1)
A.fn = lambda x : x^2
f= A.fn
f(2)

In other words, I want to make A.fn a reference to a function not
known to me at the time I define class A. I want to be able to
initialize it later.


From: MRAB on
Terry Reedy wrote:
> On 4/27/2010 7:36 PM, GZ wrote:
>> I want to store a reference to a function into a class property.
>>
>> So I am expecting that:
>>
>> class A:
>> fn = lambda x: x
>>
>> fn = A.fn
>> fn(1)
>>
>> Traceback (most recent call last):
>> File "<string>", line 1, in<string>
>> TypeError: unbound method<lambda>() must be called with A instance as
>> first argument (got int instance instead)
>>
>>
>> The problem is that A.fn is treated as a bounded method. I really want
>> A.fn to be a variable that stores a reference to a function. Is there
>> any way to achieve this?
>
> Use 3.1, though you will have the same issue when calling it and same
> fix that Chris gave.
>
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> class A:
fn = lambda x: x


>>> fn = A.fn
>>> fn(1)
1