Guido's new method definition idea
Lie Ryan
lie.1296 at gmail.com
Tue Dec 9 16:28:37 EST 2008
On Mon, 08 Dec 2008 20:55:16 +0000, Arnaud Delobelle wrote:
> anthony.tolle at gmail.com writes:
>
>> class C:
>> def createfunc(self):
>> def self.func(arg):
>> return arg + 1
>>
>> Or, after the class definition is done, to extend it dynamically:
>>
>> def C.method(self, arg):
>> self.value = arg
>>
>> ...which would be the equivalent of the following:
>>
>> def method(self, arg):
>> self.value = arg
>> C.method = method
>
> What about the following then?
>
> functions = {}
>
> def functions['square'](x):
> return x*x
>
> def functions['cube'](x):
> return x**3
>
> --
> Arnaud
-1. In most cases, it would generally be better as:
def functions(x):
return x.func()
since the rule-of-thumb of OOP is that objects should do things by
itself. This is also more in line with python's built-in functions, which
merely calls the appropriate __special__ names.
and whenever creating an object is too complex, that could easily be
def functions(which, x):
def _function_square():
return x*x
def _function_cube():
return x**3
d = {'square': _function_square,
'cube': _function_cube,
}
return d[which]()
More information about the Python-list
mailing list