Two minor syntactic proposals
Bengt Richter
bokr at accessone.com
Wed Jun 20 14:54:59 EDT 2001
On Mon, 18 Jun 2001 02:07:54 -0400, barry at digicool.com (Barry A.
Warsaw) wrote:
>
> GM> Suppose you have a class and want to add a new method to
> GM> it. (Why? It's someone else's, you aren't allowed to change
> GM> the source, and subclassing won't do because these things are
> GM> created by code you don't control.)
>
> GM> You can do it, thus:
>
> | def new_method(self, x):
> | something_else()
> | return x+1
> | Thing.new_method = new_method
>
>Long ago, I propsed being able to do something like
>
>class Thing:
> pass
>
>def Thing.new_method(self, x):
> something_else()
> return x+1
>
>No new syntax is necessary, and it would allow more separation of
>interface and implementation, allowing the /interface/ to be the class
>definition, maybe even scarecrow-ish in nature, while the
>implementation could be completely separated into a separate file, or
>farther down in the same file.
>
>It never caught on, but this was in the (way) pre-PEP days. If
>there's interest, I could write up a PEP on this.
I think my enhanced lambda version of def would work in this
context too:
class Thing:
pass
Thing.new_method=def(self, x):
something_else()
return x+1
More information about the Python-list
mailing list