Using function parameters to determine method kind

Lenard Lindstrom len-1 at telus.net
Mon Apr 5 02:27:06 EDT 2004


I was wondering if anyone has suggested having Python determine
a method's kind from its first parameter. 'self' is a de facto
reserved word; 'cls' is a good indicator of a class method
( __new__ is a special case ). The closest to this I could find
was the 2002-12-04 posting 'metaclasses and static methods'
by Michele Simionato. The posting's example metaclass uses the
method's name. I present my own example of automatic method
kind declaration. Being a module with some support functions
and a test case it is too large to post so is available online at:

   http://www3.telus.net/len_l/Python_Explorations.html

as dparams.py .

# dparams.py example
from dparams import Object
class C(Object):
    def __init__(self, x): # instance method
        self.x = x
    def MyClass(cls): # class method
        print cls
    def MyStatic(a): # static method
        print a
    def MyStatic2(self):
        print self
    MyStatic2 = staticmethod(MyStatic2) # still works


Lenard Lindstrom
<len-l at telus.net>



More information about the Python-list mailing list