why is there no class (static) methods in Python ?
mlunnay at ihug.com.au
Mon Jun 18 08:36:11 CEST 2001
why do you need it as part of the class, if it is related to the class just
have it in the same module so you can do:
x = foo.foo()
"Richard Gruet" <rgruet at intraware.com> wrote in message
news:3B2D6669.7DEFD13F at intraware.com...
> Martin von Loewis wrote:
> > Richard Gruet <rgruet at intraware.com> writes:
> > > I (and other people on the Python french mail list) wonder why there
> > > are no class (static) methods in Python. You cannot define a really
> > > unbounded (ie to an instance) method within the namespace of a class.
> > First of all, you *can* define such a thing if you absolutely want, see
> > http://www.python.org/doc/FAQ.html#4.84
> Thanx for the info. But the FAQ confirms that there is no way to define
> a class method. What is recommended (define a module fct) is already what
> > > It's not natural nor elegant to have to create an instance to call a
> > which is not related to a particular instance. We would like to be able
> > write things like:
> > >
> > > class C:
> > > def staticFoo(x): print x
> > >
> > > C.staticFoo(1)
> > I'd like to question why you want to do this. Isn't it much better to
> > class C:
> > pass
> > def staticFoo(x):
> > print x
> > I.e. what has class C to do with staticFoo?
> Of course, my example is fictitious and was only intended to show the
> syntax. But the reason to choose to define a function as a class method
> than a mere (static) function is -obviously- when this function is closely
> related to the class itself, not to one of its instances. Some languages
> smalltalk even define classes as instances of metaclasses, which makes
> Then you can see class methods like methods of instances that are actually
> In fact, constructors (and destructors) are class methods, not instance
> methods, but they are handled specially in the language so they appear as
> instance methods..
> Typical examples of class methods:
> loadInstanceFromStream(aStream) # Create an instance from its
> state read on a stream
> getInstanceCount() # returns the number of
> of this class
> getInstanceList() # returns the list of
> instances of this class
> getClassName() or getAnyInfoOntheClass() ......
More information about the Python-list