class methods?

Tres Seaver tseaver at aftershock.neosoft.com
Wed Jan 12 11:47:36 EST 2000


In article <20000112145448.29699.qmail at web804.mail.yahoo.com>,
Stuart Zakon  <sjz18 at yahoo.com> wrote:
>Is there no such thing as class scope methods in Python? (Analogous to static
>methods in Java, C++).
>
>I have a class scope dictionary that I am using to collect statistics on method
>invocations of the class instances and I wanted to have a class method to print
>them out. 
>
>However, Python seems to want a class instance to call a method. Does every
>method on a class need self as a parameter?
>
>Certainly I could make a separate statistics class and instantiate it once;
>however, the nature of the problem is such that it is more natural for the
>statistics collection to be private to the original class.
>
>If I am missing something, I apologize, but coming from Java and C++ it just
>seemed natural to have this feature. If there can be class attributes, then why
>not class methods? My understanding is that this is natural for Smalltalk as
>well.

This one surprised me too, coming to Python from C++/Java.  The usual response
in Python is that class methods solve a problem (a need to access "private"
class data) which doesn't exist in Python -- class members are always
accessible.  The usual cognate is to declare a module-scope function
which does the work of the class method.
-- 
---------------------------------------------------------------
Tres Seaver           tseaver at palladion.com       713-523-6582
Palladion Software    http://www.palladion.com



More information about the Python-list mailing list