Encapsulation in Python
Or, “I see London, I see France, I see Python’s underpants”
I come from a primarily C# background, where I have fairly tight control over how visible my properties and methods are. I can choose to make them private to the class, protected to the class and its children, internal to the assembly or public for everything.
In my course of learning Python, I’ve discovered awesome things like docstrings, multiple inheritance and sequence unpacking. However, I’ve also been shocked and made more than a little uncomfortable by the realisation that Python doesn’t implement encapsulation. I don’t trust other people not to abuse access to methods that I’ve intended to be private – I’ve seen too much bad code for that. While I know there are ways to get around this, I don’t feel I should have to perform code gymnastics to implement what I feel is an essential part of an OO language.
Another thing that makes me cringe a little about Python is the need to pass in the self parameter for all object methods. I realise that this is required to differentiate between an instance method and a class/static method, however I just can’t shake the idea that it seems a little clunky, even though I know it’s no different from writing a static at the start of my function definition.
However, to wrap up the last two weeks, I feel confident in saying that from what I’ve seen I like Ruby more than I like Python. That’s not to say these are bad languages; I first learnt web based programming with PHP, and I’m sure that I’m going to leave PHP in the dust for one of these two languages. I will also disclose that I did not learn Python 3.0, and haven’t really looked into it whatsoever, so if 3.0 changes some of the stuff here, forgive me.
Having said that, I will investigate both languages more, and definitely work in both more. I’ve found PHP doesn’t do it for me anymore, I’ve seen a better side of programming where I don’t have to live with ugly warts in my languages, and PHP is ugly. Long live Python and Ruby.