How to use a class property to store function variables?

G

GZ

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
 
C

Chris Rebert

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
 
T

Terry Reedy

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
 
G

GZ

Hi Chris,

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.
 
M

MRAB

Terry said:
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. fn = lambda x: x

1
 
A

alex23

GZ said:
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.

It always helps to try an idea out before dismissing it out of hand.
Experimentation in the interpreter is cheap and easy.
.... fn = staticmethod(lambda x: x*x)
....3

However, I'm assuming you're wanting to do something like this:
.... def act(self):
.... print self.fn()

That is, providing a hook in .act() that you can redefine on demand.
If so, note that you only need to decorate functions as staticmethods
if you're assigning them to the class. If you intend on overriding on
_instances_, you don't:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
three

Incidentally, this is known as the Strategy pattern, and you can see a
simple example of it in Python here: http://en.wikipedia.org/wiki/Strategy_pattern#Python

Hope this helps.
 
G

GZ

It always helps to try an idea out before dismissing it out of hand.
Experimentation in the interpreter is cheap and easy.


...   fn = staticmethod(lambda x: x*x)
...>>> A.fn(10)
100

3

However, I'm assuming you're wanting to do something like this:


...   def act(self):
...     print self.fn()

That is, providing a hook in .act() that you can redefine on demand.
If so, note that you only need to decorate functions as staticmethods
if you're assigning them to the class. If you intend on overriding on
_instances_, you don't:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>


three

Incidentally, this is known as the Strategy pattern, and you can see a
simple example of it in Python here:http://en.wikipedia.org/wiki/Strategy_pattern#Python

Hope this helps.

Ah, this totally works. The key is to use the staticmethod function.
Thanks a lot.
 
G

GZ

It always helps to try an idea out before dismissing it out of hand.
Experimentation in the interpreter is cheap and easy.


...   fn = staticmethod(lambda x: x*x)
...>>> A.fn(10)
100

3

However, I'm assuming you're wanting to do something like this:


...   def act(self):
...     print self.fn()

That is, providing a hook in .act() that you can redefine on demand.
If so, note that you only need to decorate functions as staticmethods
if you're assigning them to the class. If you intend on overriding on
_instances_, you don't:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>


three

Incidentally, this is known as the Strategy pattern, and you can see a
simple example of it in Python here:http://en.wikipedia.org/wiki/Strategy_pattern#Python

Hope this helps.

Another question: I am not sure how staticmethod works internally. And
the python doc does not seem to say. What does it do?
 
C

Chris Rebert

Another question: I am not sure how staticmethod works internally. And
the python doc does not seem to say. What does it do?

It involves the relatively arcane magic of "descriptors".
See http://docs.python.org/reference/datamodel.html#implementing-descriptors
or for a more complete but advanced explanation, the "Static methods
and class methods" section of
http://www.python.org/download/releases/2.2.3/descrintro/

Understanding exactly how staticmethod() and friends work is not too
essential in practice though.

Cheers,
Chris
 
G

GZ

It involves the relatively arcane magic of "descriptors".
Seehttp://docs.python.org/reference/datamodel.html#implementing-descriptors
or for a more complete but advanced explanation, the "Static methods
and class methods" section ofhttp://www.python.org/download/releases/2.2.3/descrintro/

Understanding exactly how staticmethod() and friends work is not too
essential in practice though.

Cheers,
Chris
--http://blog.rebertia.com- Hide quoted text -

- Show quoted text -

Got it. I appreciate your help.
 
B

Bruno Desthuilliers

GZ a écrit :
(snip)
Ah, this totally works. The key is to use the staticmethod function.

staticmethod is not a function, it's a class.
Another question: I am not sure how staticmethod works internally. And
the python doc does not seem to say. What does it do?

It's easy to figure this out once you understand how Python turns
functions into methods: http://wiki.python.org/moin/FromFunctionToMethod

Basically, the staticmethod type acts as a descriptor wrapper around the
function object that short-circuits the function object's own descriptor
implementation and yields the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top