Is my thinking Pythonic?

bearophileHUGS at bearophileHUGS at
Thu Aug 21 14:36:59 CEST 2008

Hussein B:
> class Car:
>   def setspeed(self, speed):
>      self.speed = speed
>   def setbrand(self, brand):
>      self.brand = brand

You can also learn the _attribute and __attribute conventions.
In Python getter/setters are used less often, you can remove those two
setters and just access the attributes from outside.
Later, in derived classes, if you want to make their workings more
complex you can add properties.
Note that Python isn't able to inline things as HotSpot does, so each
getter/setter (or dot: is slower than you use
your code slows down.

> So, I created this constructor:
> --
> def __init__(self):
>   self.speed = None
>   self.brand = None
> --
> This way, I can figure the instance variables by just reading the
> __init__ method.

Sometimes I do the same thing, to to "document" the attributes used.


More information about the Python-list mailing list