Is my thinking Pythonic?
bdesth.quelquechose at free.quelquepart.fr
Thu Aug 21 22:34:22 CEST 2008
Craig Allen a écrit :
> generally, I name the members in the Class definition and set them to
> None there...
> class Car:
Unless you have a really good reason to use an antiquated and deprecated
object model, use "new-style" classes (for a value of "new" being "now
many years old"):
> speed = None
> brand = None
> def __init__():
> self.speed = defaultspeed #alternately, and more commonly, get
> this speed as a initializer argument
> self.brand = defaultbrand
Please dont. This is a useless repetition, a violation of the DRY
principle, and a potential waste of time for the maintainer who will
wonder what these class attributes are for.
> That solves the issue of being able to "see" all the members of an
> object by reading code...
> however, this all goes out the window when
> composing an instance dynamically (i.e. metaclass type stuff).
No need for metaclasses here. Instances are *always* composed
dynamically. While I agree that setting all attributes to a sensible
default in the initializer is *usually* good style (even if it's
technically useless and possibly error prone), duplicating them all as
class attribute is at best a WTF (IMHO and all other disclaimers here...).
More information about the Python-list