On Dec 6, 4:15 pm, Carl Banks <[EMAIL PROTECTED]> wrote: > On Dec 6, 12:47 am, "Patrick Mullen" <[EMAIL PROTECTED]> wrote: > > > Could I do something like this: > > > def a.add(b): return a+b > > > Outside of a class? Of course then that makes you think you could do > > 5.add(6) or something craaaazy like that. (I mean, you can do > > (5).__add__(6) but that's something else entirely) > > I'd be inclined to think that this defines an instancemethod on an > existing object a. In other word, I'd read the following two lines as > more or less equivalent. > > def a.add(b): return a+b > > a.add = lambda b: a+b > > Just as the following are equivalent: > > def foo(): return bar > > foo = lambda: bar > > I had been -0 on this, but now I think I'm -1.
This brings up another question, what would one use when referencing method names inside the class definition?: class C: def self.method(arg): self.value = arg def self.othermethod(arg): self.value = arg # do this? funcs = (self.method, self.othermethod) # or this? funcs = (method, othermethod) On another related note, I would be interested in seeing this syntax adopted for the very purpose Carl hinted at... Normally, if I'm defining a nested function that needs to be stored as an object attribute, I have to use a dummy name, like the following: class C: def createfunc(self, arg): def _dummy(arg): return arg + 1 self.func = _dummy It would be nice to be able to do the following instead: 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 Since functions are first-class objects, it seems perfectly reasonable to me. -- http://mail.python.org/mailman/listinfo/python-list