[Chicago] Get/Set/Accessors in Python?

Philip Doctor diomedestydeus at gmail.com
Wed Dec 5 18:02:53 CET 2012


> Possibly because I try very hard to put less logic on the objects
themselves and more in the functions/methods that use them

Without trying to further transform Sheila into the hulk, I would suggest
that's one way to do it, but another way gets are the heart of OO
encapsulation and data hiding.

If a Car class owns the idea of being a car, then it should own private int
speed as well as the way to set speed (either through public void
accelerate() or a public setter).  Then a call to those methods invokes the
business logic in Car because Car owns that business logic, after all, it
is the abstraction of a Car.  Now when we goto change how a Car works and
want to check that the engine is On before we can set speed, we have one
place to maintain that code, in Car, and none of the other developers who
call our code need to make any changes (unless a developer has a simulation
of rolling a car down a hill... then maybe we are in trouble... hopefully
we have integration tests).  On the other hand, if Car just has a lot of
data in it, and you are passing Car to a bunch of functions which then
change the variables in Car, then the impact of adding an "On" check is
maybe less certain.  There's a lot of other code you need to search through
for references to Car and see how each one is using Car.

Having seen both ways of doing it for many years, I can agree that both are
going to have drawbacks, and both are going to be subject to bad coding
practices.  The most obnoxious of which is easily some developer changing
Car by copy+pasting accelerate() into accelerate_new() and then making
their modifications there (if you want to talk hulk rage, that's mine right
there).  So neither is a magic bullet, but having car data hide/etc is the
more object oriented approach.

Interestingly, one of my favorite blogs by Mark Seemann (who is a
dependency injection master) recently had an interesting discussion on
encapsulation and information hiding as well.  I would highly recommend his
take on it too (although there are many great articles on the web):

http://blog.ploeh.dk/2012/11/27/EncapsulationOfProperties.aspx






On Wed, Dec 5, 2012 at 10:36 AM, sheila miguez <shekay at pobox.com> wrote:

> Btw, yes.
>
> On Wed, Dec 5, 2012 at 9:38 AM, Malcolm Newsome
> <malcolm.newsome at gmail.com> wrote:
> >
> > Hopefully these questions are clear enough...
>
>
>
> --
> sheila
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20121205/1a46057b/attachment.html>


More information about the Chicago mailing list