static python classes ?

Neil Cerutti horpner at yahoo.com
Wed Jun 20 09:41:58 EDT 2007


On 2007-06-19, Bjoern Schliessmann
<usenet-mail-0306.20.chr0n0ss at spamgourmet.com> wrote:
> Diez B. Roggisch wrote:
>> With other OOP languages you mean Java. Which does have static
>> methods because they lack the notion of a function by its own,
>> so the shoehorned them into their "everything is inside a
>> class"-paradigm.
>
> ACK, but doesn't C++ have static methods too?

C++ does have what it calls "static member functions".

class foo
{
  public:
    static void bar() { /* nada */ }
};

Python names a similar thing a @staticmethod (the name taken from
C++, obviously, since C++ originated the essentially meaningless
word-grouping "static member function" in order to reuse an
existing keyword).

// It may be called with two different syntaxes, as in Python:

void goo()
{
  // ...using the qualified name:
  foo::bar();
  // ...or with an instance of the class:
  foo f;
  f.bar();
}

In C++ they are used most often for factory functions, since they
conveniently have access to the class's private members, and
don't want or need an existing instance. Python seems to have
adopted this use-case (ConfigParser, for example), but without a
need for it (code organization?).

-- 
Neil Cerutti
I am free of all prejudices. I hate everyone equally. --W. C. Fields



More information about the Python-list mailing list