Classmethods are evil
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Mon May 19 05:29:48 EDT 2008
Ivan Illarionov a écrit :
> After re-reading "Python is not Java" I finally came to conclusion that
> classmethods in Python are a very Bad Thing.
>
> I can't see any use-case of them that couldn't be re-written more clearly
> with methods of metaclass or plain functions.
Plain functions don't give you polymorphic dispatch and can't be
overriden. Using metaclass methods requires a custom metaclass, which 1/
adds boilerplate code and cognitive load and 2/may bring problems,
specially wrt/ MI.
> They have the following issues:
> 1. You mix instance-level and class-level functionality in one place
> making your code a mess.
May I remind you that every name defined at the top-level of a class
statement becomes a class attribute ?
Anyway : most of the use case I've had so far for classmethods required
collaboration between the instance and the class, and I definitively
prefer to have related functionalities defined in one place.
> 2. They are slower than metaclass methods or plain functions.
Slower than plain functions, indeed - but so are instancemethods. Slower
that metaclass methods ? I never benchmarked this, but I don't see any
reason that should be the case. Anyway, you could also argue that
properties are slower than plain attributes, which are slower than local
vars etc...
> I really want to hear your opinions on the subject.
Mine is that classmethods are a GoodThing that make simple thing easy.
More information about the Python-list
mailing list