static class methods and data members

Neil Zanella nzanella at cs.mun.ca
Thu Aug 19 21:23:03 CEST 2004


Phil Frost <indigo at bitglue.com> wrote in message news:<mailman.1910.1092869488.5135.python-list at python.org>...
> take a look at classmethod and staticmethod with pydoc, in the docs, or
> help(). Static data members are done like so:
> 
> Class C:
>   iAmStatic = 1
>   def __init__(self):
>     self.iAmNotStatic = 2
> 
> Thinking of this in terms of "static" and "automatic" isn't the way to
> go, really. That's C's way of doing it, and it's in Java only because
> Java is nothing more than C.
> 
> Here's how it works: There are classes, and there are instances of those
> classes. Each has a namespace. when one does <instance>.name, "name" is
> located first in the instance namespace, then in the class namespace.
> 
> However, assignment is different. Assignment just looks in the instance
> namespace, and if it's not there, it creates it. Thus if one were to do
> "self.iAmStatic" in a method of C, that does not change the value of
> iAmStatic for all instances of C. However, one can do "C.iAmStatic"
> anywhere, and that will change "iAmStatic" for all instances of C that
> do not have 'iAmStatic' bound to something at the instance level.
> 
> The best way to understand this is through experimentation in the
> interactive interpreter. To make things easier, you can peek inside
> python's implementation by doing C.__dict__ or anInstanceOfC.__dict__ to
> see the dictionary that represents that namespace.

Phil,

Your explanation is so clear, it should be included in just about any
book on Python!!! Thank you so much!!!
 
> After you have absorbed this, you will probably see the use of
> classmethod, as it's not immediately apparent if you aren't familiar with
> Python. Happy hacking!

The http://www.python.org/2.2.1/descrintro.html#staticmethods you mentioned
really does contain some of the information I was missing pertaining to
classmethod and staticmethod. I wonder how come this file was not
installed on my Fedora Core 2 Linux distro. I also notice that
there is no corresponding file with the same name located at
http://www.python.org/doc/2.3.4/. The document seems to have
been removed from later Python documentation versions... how
come?

So, unlike Python classmethods which are roughly the equivalent
of C++ static class methods, Python staticmethods really know nothing about
the members of the class that encloses them and act pretty much like external
defs. So what's the advantage of a staticmethod over an external def?

The cls first argument to a Python classmethod is analogous to the self
first argument to __init__: when you say Class.cmethod() the Class instance
is passed as the first parameter to the class method, and when you say
instance = Class(), the instance being constructed is passed to __init__
as self, and when you say instance.imethod(), the instance is passed to
imethod() as the first parameter, self. The parameters cls and self are
barely named so by convention: they are not keywords in Python, unlike
say the this pointer which is a keyword in C++.

While qualification within a class is optional in languages like C++ and
Java, in Python you must always qualify instance variables with self within
instance methods, and always qualify class variables with cls within Python
class methods.

Since python static methods know nothing about a class, they are equivalent
to methods defined outside a class, except they may be called with the class
as the first argument (for no good reason).

Feedback, reclarification, or other,
on the above comments greatly appreciated.

Thanks,

Neil

#!/usr/bin/python

class Foo:
  x = 0
  y = 1
  def foo(cls):
    print "classmethod: hello"
    print cls.x
  foo = classmethod(foo)
  def bar():
    print "staticmethod: hello"
    print Foo.x
  bar = staticmethod(bar)

if __name__ == "__main__":
  Foo.foo()
  Foo.bar()



More information about the Python-list mailing list