Michael Torrie wrote:
Colin J. Williams wrote:
def fun( ., cat):
I don't see the need for the comma in fun.
It (the entire first variable!) is needed because a method object is
constructed from a normal function object:
def method(self,a,b):
pass
class MyClass(object):
pass
MyClass.testmethod=method
That's precisely the same as if you'd defined method inside of the class
to begin with. A function becomes a method when the lookup procedure in
the instance object looks up the attribute and returns (from what I
understand) essentially a closure that binds the instance to the first
variable of the function. The result is known as a bound method, which
is a callable object:
instance=MyClass()
instance.testmethod
<bound method MyClass.testmethod of <__main__.instance object at xxx>>
How would this work if there was not first parameter at all?
In short, unlike what most of the implicit self advocates are saying,
it's not just a simple change to the python parser to do this. It would
require a change in the interpreter itself and how it deals with classes.
An interesting example. I would think that, for the common case, where
a method
is defined within a class statement, this would not be difficult.
Others have
suggested a pre-processor to illustrate this.
OTOH, the assignment of a function to a class, so that it becomes a
method does
seem more difficult.
My play script is below.
Colin W.
# tz,py
def myFunc(a= None, b= None):
print a, b
pass
class MyClass:
pass
def myMethod1():
print 'in myMethod'
MyClass.myMethod0= myFunc
print MyClass.myMethod0
print MyClass.myMethod1
myInstance= MyClass()
print MyClass.myMethod0(myInstance)
|
--
http://mail.python.org/mailman/listinfo/python-list