comments? storing a function in an object

I V ivlenin at gmail.com
Tue Jul 21 02:24:05 EDT 2009


On Mon, 20 Jul 2009 21:53:59 -0400, Esmail wrote:
> In general I would agree with you, but in my specific case I want so
> store some additional meta-data with each function, such as the valid
> range for input values, where the max or minimum are located, the
> name/source for the function etc. I am creating list of functions for
> use in testing optimization code, so it seems best to store this data
> along with the function I am going to optimize in order to verify the
> results for a given range (for instance).

You can set attributes on functions, so if your class is just storing 
this data, rather than altering the behavior of the function, you may be 
able to just use a function:

def f(x, y):
	"x * y"
	return x * y

f.arguments_max = [1000, 255]

A function also already knows how many arguments it takes, which you can 
access as f.func_code.co_argcount



More information about the Python-list mailing list