Learning Python Part1

So I decided to start learning Python. This post is simply a walk-through of how I’m going on learning Python.
What I write here, thoughts and such, might be incorrect and/or inaccurate. You could see it as a “Poor man’s guide to learn Python”.

Coming from Java and C#, my first contact with Python was rather strange, it required me to use a different mind-set.

The easiest way for me to understand Python, was to actually think in PHP, meaning, that all my functions had to be defined at the beginning before trying to launch them.

This will not work:

def sayHi():
    print 'Hello World!'

This will work:
def sayHi():
    print 'Hello World!'


Python doesn’t use enclosing characters like { } to define the scope of methods, conditions or loops, but is heavily based on indentation.

When creating a new Python project in NetBeans, NetBeans fills in your newly created file with a strange template:

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.

__date__ ="$8-feb-2010 16.51.51$"

if __name__ == "__main__":
    print "Hello World";

That special IF-condition is a special trick so you can both execute the file as a normal Python script, but can also be used as a library.

So now for the part that is of most interest to me: Object Oriented Python.

In Java and C#, I’m used to thinking a Class as an Object. In Python, there seems to be a distinction between an Class variable, and an Object variable. So instead of an Object being made from a Class, in Python it seems like a Class is kind of a container for Objects.

Let me make this clear with an example:

class Person:
    population = 0

    def __init__(self, name):
        '''Initializing person's data.'''
        self.name = name
        print '(Initializing %s)' % self.name

        #Person is created,
        #add to population
        Person.population += 1

    def __del__(self):
        '''I'm dying.'''
        print '(Destroying %s)' % self.name

        Person.population -= 1

        if Person.population == 0:
            print 'There is no one left'

personObj1 = Person('Sarah')
personObj2 = Person('Jane')
personObj3 = Person('Jessi')

The keyword self has to be added as a parameter of every Function you make. You could see it as the keyword “this” in Java and C#.

As you might assume, __init__ and __del__ are the Standard Constructor (when the object is created) and the Destructor (when the object is eliminated).

The variable “population” is a Class Variable, this seems to behave as a static variable like in Java and C#, meaning that everything can access it out of the Object.

Now here’s the interesting part: all of a sudden there is this self.name, this behaves as a dynamical made Object Variable. In Java and C#, you’d expect this to be in the Field of your Class, as a propriety.

So here is what happens:
When the object are created, they all get a name like: “Sarah”, “Jane” and “Jessi”.
With each object created, the all increment the population field by 1.
When the program is done, it will destroy the objects, and thus, the population variable will be decreased.

This is the output of this program:

(Initializing Sarah)
(Initializing Jane)
(Initializing Jessi)
(Destroying Sarah)
(Destroying Jane)
(Destroying Jessi)
There is no one left