list.sorted()

Robert Brewer fumanchu at amor.org
Sun Dec 7 06:28:10 CET 2003


Douglas Alan wrote:
> I'm not sure what your reason is for making a big distinction here
> between class methods and static methods.  To me, making a big
> distinction here would make Python less regular, and consequently more
> difficult to learn and understand.  Also, it means that if I were to
> decide to change isLegalTag from a static method to a class method, I
> would have to change more code.

[Answering in reverse order] I don't see a difference between them--they
both exist at the class level, ignoring their instances. I'm not sure
there's a reason to call either of them from an instance; IMO it's
syntactic sugar that obscures what's really going on. In the example
which Raymond posted:

    [f(elem, arg) for elem in [3,2,1].sorted(anotherseq)]

that leads directly to confusion, since one might expect [3,2,1] to have
some effect upon .sorted(). The same applies to staticmethod.

> But in Python, I can get rid of vIsLegalTag() altogether, and just do:
> 
>      print f.isLegalTag("baz")
> 
> If Python were changed as you suggest (modulo "classmethod" vs
> "staticmethod") I would have to change the above code.  The obvious
> way, to me, would be to replace "f." with "f.__class__.".
> 
> Now what is the alternative to this that you would do?

I think in this case, I'd say that's an entirely appropriate choice:
replace "f." with "f.__class__". Of course, this already happens "behind
the scenes" when you call a classmethod on an instance--the class gets
"magically" sent to the method instead of the instance. I'm simply
wondering if, in the interest of avoiding confusion, it would be better
to not have this sleight-of-hand built in. It's a semantic issue more
than a structural one, I think. I wanted to know if anyone had come
across a case where it DID raise structural issues; for example, some
Python metareality where f.__class__ could not be determined (or
called?), or where someone was using classmethods and hand-passing the
first param (which ISTR isn't allowed), or similarly wierd stuff.
Stranger things have happened.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list