[Python-ideas] class-only methods without using metaclasses
steve at pearwood.info
Fri Mar 8 11:45:42 CET 2013
On 07/03/13 19:21, Nick Coghlan wrote:
> On Thu, Mar 7, 2013 at 5:10 PM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> It's too much additional complexity to resolve a largely theoretical
> problem. Since class methods can be shadowed in instances, the fact
> they're accessible through the instances really doesn't hurt anything,
> and the distinction between a class method and a class only method
> would be too subtle to easily explain to anyone not already steeped in
> the details of descriptors and metaclasses.
Surely that only applies to the implementation, not the concept itself?
The interface is trivially easy to explain to anyone even half-way familiar
with Python's OO model.
Class methods are accessible from either the class or the instance, and
receive the class (not the instance) as the first argument.
Class-only methods are only accessible from the class.
I'm not sure what use class-only methods are or what problems they solve,
apart from a general dislike of being able to call classmethods from an
instance. I can't think of any case where I would want to actively prohibit
calling a classmethod from an instance, so I don't know that this actually
solves any problems. But it looks interesting and I think Eric should
put it up as a recipe on ActiveState.
More information about the Python-ideas