why is there no class (static) methods in Python ?

James_Althoff at i2.com James_Althoff at i2.com
Tue Jun 19 14:36:10 EDT 2001


Thomas Heller wrote:
><James_Althoff at i2.com> wrote in message
>> My own metaclass experiments and posts have all tried to provide a
>> mechanism for true class methods.  But all metaclass mechanisms in
Python
>> that I'm familiar with are awkward, IMHO -- and probably don't and won't
>> get used much (if at all).
>I'm really satisfied with the following recipe (you will probably
>remember):
>
>class ClassMeta:
>    class Instance:
>        def __init__(self, ...):
>            # initialize the new instance
>            ....
>
>        def inst_method(self):
>            # an instance method
>
>    def new(self, *args):
>        # a class method acting as a constructor
>        return self.Instance(arg1, arg2):
>
>    def a_class_method(self, ...):
>        # another class method
>        ....
>
>
>Class = ClassMeta()
>
>It is easy to understand and simulates 'real' class methods very well.
>I will certainly use them.
>
>What's wrong with this pattern?

I agree that the pattern above does a good job of "simulating 'real' class
methods".

The (slight) awkwardness here (for me) is that you have to keep in mind
that "Class" refers to the metaclass instance and not to the "class of
interest" ClassMeta.Instance.  If you only had to deal with
ClassMeta.Instance inside of the class methods (using self.Instance) there
wouldn't be much of an issue.  But if you want to subclass
ClassMeta.Instance then you need a direct reference to it (the actual
class) for use in the class statement and in all methods that require a
call to a superclass method.  So in the general case you probably want to
expose ClassMeta.Instance via something like:
"MyClass = Class.Instance" or "MyClass = ClassMeta.Instance"
so you can say
class MySubClass(MyClass)"
instead of
"class MySubClass(Class.Instance)" or "class
MySubClass(MetaClass.Instance)"
and
"MyClass.instanceMethod(self)"
instead of
"Class.Instance.instanceMethod(self)" or
"ClassMeta.Instance.instanceMethod(self)"

Summarizing, the extra things you do in the above:
o use an outer class for the metaclass definition
o use an inner class for the actual class definition
o manually create a singleton instance of the metaclass
o expose the inner class via an assignment (I would think)
o keep three names and objects in mind:
    * ClassMeta -- you need to know this to subclass the metaclass itself
    * Class -- you call class methods on this metaclass instance
    * MyClass -- you want this when subclassing and calling superclass
methods

That's not so bad all things considered.  But it's enough extra stuff that
it might prevent many/most from using it as the standard idiom for creating
classes.  I would like to see a standard idiom that you could use whenever
you create *any* class so that you can start with no class methods and then
later add one (or more) in easily without having to change the basic
mechanism you used for creating the class to begin with.

Despite these concerns, I agree that the pattern above is a good one.  :-)

Jim





More information about the Python-list mailing list