Guido's new method definition idea

Aaron Brady castironpi at gmail.com
Mon Dec 8 16:22:14 EST 2008


On Dec 8, 2:55 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> anthony.to... 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

I agree that it's an extension (extrapolation) of the 'def self.meth'
notation.  For 'func_name', would you use the quoted string?  Would
you allow arbitrary expressions for the string, and the dictionary to
add to?  For example:

def dict_val( )[ "name%i"% int_val( ) ]( self, arg, arg, arg ): ...

It's not much worse than: (Unproduced)

def anon( self, arg, arg, arg ): ...
dict_val( )[ "name%i"% int_val( ) ]= anon

#or if you really want the target before the def:

@assign( dict_val( ), "name%i"% int_val( ) )
def _( self, arg, arg, arg ): ...

And the latter are already both legal.  But there have been PEPs
accepted before that abbreviate 2 lines into 1.



More information about the Python-list mailing list