[Python-3000] The case for unbound methods?
Steven Bethard
steven.bethard at gmail.com
Sun Mar 9 01:38:45 CET 2008
On Sat, Mar 8, 2008 at 4:33 PM, Anthony Tolle <artomegus at gmail.com> wrote:
> On Sat, Mar 8, 2008 at 3:28 PM, Steven Bethard <steven.bethard at gmail.com> wrote:
> > Why is it so crucial that "self" is the first argument? If I use a
> > decorator that adds a new element to the beginning of the argument
> > list, I wouldn't be surprised that I now have to write my methods as::
> >
> > @add_initial_argument
> > def method(new_arg, self, ...):
> > ...
>
> That would work with class binding in Python 3.0 (i.e. "unbound"
> methods). In Python 2.5, doing that with an unbound method would
> throw an error.
>>> def add_initial_argument(func):
... def f(*args, **kwargs):
... return func('newarg', *args, **kwargs)
... return f
...
>>> class C(object):
... @add_initial_argument
... def foo(newarg, self):
... print newarg
...
>>> C().foo()
newarg
>>> C.foo(C())
newarg
I don't see an error thrown with either the bound or unbound methods...
STeVe
--
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
--- Bucky Katt, Get Fuzzy
More information about the Python-3000
mailing list