Is my thinking Pythonic?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Aug 21 08:36:59 EDT 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: foo.bar.baz is slower than foo.bar) 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.

Bye,
bearophile



More information about the Python-list mailing list