list.sorted()

Robert Brewer fumanchu at amor.org
Sat Dec 6 16:15:45 EST 2003


On python-dev, Raymond Hettinger wrote:
> When working on itertools.groupby(), the tutorial updates, and
> exercising the newer features, I've made frequent use of 
> list.sorted().
...
> Part of the reason for making it a classmethod was that it *is*
> effectively an alternative constructor for lists.  OTOH, that could be
> said for any function that returns a list.
> 
> The two disadvantages of having it as a classmethod are the mandatory
> list dot prefix and that it is confusing when called with an existing
> list:  
> 
>   [f(elem, arg) for elem in [3,2,1].sorted(anotherseq)]

My question for python-list is:

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? Notice that the
reverse is not true: you can't call normal instance methods from the
class--you get a TypeError by not passing an instance for "self" to bind
to.

If nobody can come up with one, perhaps classmethods shouldn't be
callable from instances. IMO, that's the root of the "confusion" in the
example.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list