static class methods in Python?

Neel Krishnaswami neelk at brick.cswv.com
Sat Feb 19 03:03:45 CET 2000


Aahz Maruch <aahz at netcom.com> wrote:
> In article <slrn8arn3d.9md.neelk at brick.cswv.com>,
> Neel Krishnaswami <neelk at alum.mit.edu> wrote:
> >
> >This is probably a stupid question, but what /is/ a class method?
> >I've never programmed in C++ (or Java), so an explanation would be
> >appreciated.
> 
> Class methods are used to access and modify class variables.  For
> example:
> 
> class foo:
> 	bar = None
> 	def __init__(self):
> 		pass
> 	classdef isdone():
> 		if foo.bar is not None:
> 			return "!!!"
> 	classdef clear():
> 		foo.bar = None
> 
> Clearer?

Yes, thanks. 

I'd agree that the self.__class__.bar you'd have to do in real Python
is a little bit ugly. One thing I looked at doing was changing Python
so the syntax "class.foo" would expand to "self.__class__.foo" (or
whatever "self" actually is). This would solve the problem without
adding a new keyword.

However, I was scared away by the following comment in Parser/pgen.c:

	/* This algorithm is from a book written before
	   the invention of structured programming... */

:)


Neel



More information about the Python-list mailing list