

Python Black Magic - jbail
http://jeffbail.com/python-black-magic

======
meese_
This is not exactly "black magic", it's fairly well-known and explained
directly in the official documentation:
[http://docs.python.org/tutorial/classes.html#private-
variabl...](http://docs.python.org/tutorial/classes.html#private-variables).

~~~
jbail
I think developers coming to Python from other languages (like I am from a
heavy Java background) find the auto-creation of "shadow variables" pretty
interesting. To someone used to Java, being able to access a private member
via its shadow variable is a lot like black magic.

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.

~~~
piramida
Coming from C++, this is very similar to what all C++ compilers do internally
(i.e. name mangling). Python just opens a possibility to access that "mangled"
names directly, which should never be necessary unless you are doing some
metaprogramming and need full access to class internals.

------
Estragon
I learned these kinds of tricks when I started using python 12 years ago, but
I have basically never had a need for them. Perhaps my needs are simple,
though.

~~~
mccutchen
I learned this trick when I first picked up Python, too. I came from Java and,
naturally, started writing Python code in the style I had learned to write
Java: Heavily object-oriented, with getters and setters everywhere.

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.

~~~
jbail
I'm not advocating using getters/setters in Python. Just pointing out that you
can access "private" members. I don't typically use getters and setters in
JavaScript either.

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.

~~~
nivertech
One of the first textbooks on PL/1 also stated: "you should avoid using
subroutines because they they create extra overhead" ;)

40 years later and we still there ...

------
didip
While we are on the black magic thread, are there other meta-programming
tricks that people find useful/clever?

I'll share one: _import inspect_. It's handy to use during debugging.

------
lukeqsee
Forgive my incompetence, but I have never gotten the point of private
methods/variables. Could someone please explain why they exist?

Edit: I know it's slightly off-topic.

~~~
rryyan
Access specifiers (like "private") are language features used to implement the
OOP notion of encapsulation:
[http://en.wikipedia.org/wiki/Encapsulation_(object-
oriented_...](http://en.wikipedia.org/wiki/Encapsulation_\(object-
oriented_programming\)). Encapsulation is generally cited as one of the
defining features of object oriented programming.

~~~
silentbicycle
Which is weird, because it's actually a defining feature of _modular_
programming.

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.

~~~
koenigdavidmj
Yep, plenty of C modules do hacks to block access except through the defined
API. Most are content simply to use void pointers in public header files, but
some do nasty things like generating a random integer at library
initialisation time and xor'ing all returned pointers by that number before
returning it. (Eww.)

------
Jach
You can access private data in C++ too quite trivially, especially since
there's no compile-time encapsulation...

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...](http://mail.python.org/pipermail/tutor/2003-October/025932.html)
)

------
zefhous
Also, Ruby lets you call private/protected methods with Object#send.

