Archive for the ‘Python’ Category

Encapsulation in Python

Saturday, January 3rd, 2009

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 it’s 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.

Ruby & Python Bootcamp

Friday, December 19th, 2008

It’s common advice in the programming industry to learn one new language every year to stay on top of things. It’s something I honestly believe in as well. Unfortunately I can’t say I’ve learnt a new language this year.

Last year, I learnt Actionscript 3.0, which is different enough from Actionscript 2.0 to be called a new language. I did this because of work, I did it in December and I had to do it fast. That worked pretty well for me. I got thrown in the deep end and had to learn or drown.

This year, with this being my last day at work for 2 weeks, I’m going to do the same thing, but I’m going to make it a little more interesting. I consider myself a reasonably competent programmer, and fairly solid in my knowledge of OOP paradigms. I think I can learn a language pretty quick. So, during my holidays, I’m going to:

  1. See how fast I really can learn a language at least to the point where I know what to search for online and can find my own way around reasonably confidently.
  2. See just how easily various concepts from different languages transfer across syntax
  3. Build some stuff I’ve been meaning to build for a while but kept putting off

With that in mind, this year I’m going to learn both Ruby and Python in 2 weeks, dedicating a week to both languages. In the process of learning these languages, I will also build a base for 2 websites for 2 of my domains that I have been meaning to build. I won’t spend any time designing them, just building the code.

So, over the next 2 weeks, the majority of posts on here will be reflecting my thoughts on Ruby and Python, and linking to resources that I plan to use in my learning, with a final post being the unveiling of the websites I manage to build.

Wish me luck!