list.sorted()

Douglas Alan nessus at mit.edu
Sat Dec 6 19:04:34 EST 2003


"Robert Brewer" <fumanchu at amor.org> writes:

> I've been looking through my code and can't find a single case where
> I declared a classmethod, then called it from an instance, as in the
> above example. Are there any cases where this is useful?

Yes, it is useful.  I don't know if I've made use of this feature yet
in Python, but I do know that I was bent out of shape when I realized
that C++ doesn't support "virtual static functions" (because I would
have liked to use them.  Here's an example bit of C++ header from some
actual production code of mine:

  //* Class procedures:
  static MitBool                isLegalFieldTag(const RWCString& tag); 

  //* Virtual methods inherited from ObsParamItem:
  MitBool                       vIsLegalFieldTag(const RWCString& tag)

vIsLegalFieldTag() here does nothing more than call isLegalFieldTag(),
which is what I would have liked to avoid.  If C++ had had virtual
static functions, then I could have gotten rid of vIsLegalFieldTag().

These functions are used in a parser that parses text representations
of objects in the class hierarchy.  isLegalFieldTag() is used by the
parser to hlep determine whether or not a token it has just read is a
legal token.  The reason why I wanted it to be a static function is
that whether or not a token is legal does not depend on a particular
instance -- only on the class of the instance.  On the other hand, in
order to do the parsing, I do need to dispatch based on the class of a
particular instance.

I suppose, unlike in C++, in Python I could fetch the class of the
instance, and then dispatch on that, but that might be a bit
more cumbersome.

|>oug




More information about the Python-list mailing list