Is my thinking Pythonic?
Diez B. Roggisch
deets at nospam.web.de
Thu Aug 21 13:32:58 EDT 2008
Craig Allen wrote:
> generally, I name the members in the Class definition and set them to
> None there...
>
> class Car:
> speed = None
> brand = None
>
> def __init__():
> self.speed = defaultspeed #alternately, and more commonly, get
> this speed as a initializer argument
> self.brand = defaultbrand
>
>
> 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).
While I use this idiom myself, one must be cautious not to create unwanted
side-effects if anything mutable comes into play:
class Foo:
bar = []
def baz(self):
self.bar.append(2)
will *not* make bar instance-variable, but keep it as class-variable.
More information about the Python-list
mailing list