OO in Python? ^^
Alex Martelli
aleax at mail.comcast.net
Sun Dec 11 13:00:13 EST 2005
Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:
> On Sun, 11 Dec 2005 17:05:16 +0100, Matthias Kaeppler wrote:
>
> > Why would I want to use an attribute in Python, where I would use
> > getters and setters in Java?
>
> Oh boy! I've just come out of a rather long thread about that very issue.
> If you care enough to read a bunch of people arguing past each other,
> check the thread "Another newbie question", especially the posts about the
> so-called Law of Demeter.
>
> But for the short summary: suppose I write a class:
>
> class Parrot:
> def __init__(self, x):
> self._x = x
> print "please use instance.getx() and setx()"
> def getx(self):
> return self._x
> def setx(self, x):
> self._x = x
>
> What if I want to export methods of x other than get and set? Perhaps x
> is a numeric value: now I have to export a whole series of arithmetic
> operators: addx, subx, mulx, etc. And there may be other attributes I need
> to export as well...
Sorry, I don't see this.
a.setx( b.getx() + c.getx() - d.getx() )
will work just as well as (in a better-designed class) would
a.x = b.x + c.x - d.x
It's just that the former style you force syntactic cruft and overhead
which you may save in the latter. "Exporting a series of operators",
which was an issue in the LOD thread, is not one here: once you have
setter and getter, by whatever syntax, it's not germane.
Alex
More information about the Python-list
mailing list