Python Black Magic
jeffbail.com
jeffbail.com
Discovering this for the first time in Python seemed pretty cool to me. I really hope this feeling of magic in working with Python never leaves me. I want to remain amazed.
Eventually, though, I stopped trying to program in Java in Python. Once you relax and stop trying to hide your precious instance variables from all of the potentially pernicious or incompetent programmers who might be using your code (usually, in my case, just me), you no longer need tricks like this. And you realize that you never really needed them to begin with.
Java, on the other hand, kind of forces you into using getters and setters because the POJO is so ingrained into the ecosystem. That said, if you write Android apps in Java, you should avoid getters and setters because they aren't really necessary and they create extra overhead.
40 years later and we still there ...
class Foo(object):
@property
def bar(self):
print "Running getter"
return self._bar
@bar.setter
def bar(self, value):
print "Running setter"
self._bar = valueI'll share one: import inspect. It's handy to use during debugging.
Edit: I know it's slightly off-topic.
There are some other uses and examples, but this is the simplest one i can think of off the top of my head.
The idea then, is that larger teams can be divided into sub-teams, and work in parallel because the interfaces between components don't change, even if the internals change drastically. Essentially it allows components to be somewhat independent. How this works in practice tho...
Ideally, or at least for purity, all variables should be private and only accessible via get/set methods, thus encapsulation is enforced.
Public/private access are used to specify an interface to arbitrary functionality. Other modules can call a public interface, but all private internals can be removed / modified freely.
Likewise, private members of a class are implementation details I may wish to change later -- but I would like some assurance that other components will be isolated from this change, so as not to break.
Edit-add: oh, I of course forgot to mention -- for those not familiar, python permits both :-)
Guido van Rossum on the lack of private members: "We're all consenting adults here."
(And a discussion: http://mail.python.org/pipermail/tutor/2003-October/025932.h... )