Welcome to Aloril's Class Tutorial:

This is a hacked up IRC conversation regarding classes. This tutorial is for people who learn best by looking at examples rather than long winded explanations. It is assumed that before reading this tutorial you know:

1) Some basic python commands
2) About the various data types in python (ie dictionaries)
3) What a function is and how to code one.
4) What a method is (a method=something that manipulates an object).

Paste the code below into the Python command line (this won't work in IDLE!)

class person:
     def __init__(self,name,age):
     def days(self):
         return self.age*365
     def greet(self):
         print "Hello, my name is "+self.name+"."
class finnish(person):
     def greet(self):
         print "Heippa, minun nimeni on "+self.name+"."
joe=person("Joe Smith",20)
jaakko=finnish("Jaakko Lahtinen",50)
print "joe.days()==",joe.days()
print "jaakko.days()==",jaakko.days()

Type in: >>> joe=person("Joe Smith",20) >>> jaakko=finnish("Jaakko Lahtinen",50) >>> joe.greet()

Output is... Hello, my name is Joe Smith.

Type in: >>> jaakko.greet() Heippa, minun nimeni on Jaakko Lahtinen. >>> print "joe.days()==",joe.days()

Output is...

joe.days()== 7300

Type in: >>> print "jaakko.days()==",jaakko.days()

Output is... jaakko.days()== 18250

 [Pythonite] so self. is class instance object 
 that holds all attributes/member variables

[PythonNewbie] hmmm.. so the idea is that if I import self into a funtion then I get in this instance: self.name=name self.age=age all at the same time?

[Pythonite] yes and that function is defined inside class definition (==method) and thus always takes as first argument "self" and thus you always have access to all attributes class instance has like you can see from above examples inheritance: as you can see finnish version inherits __init__ and days methods and redefines greet method

[PythonNewbie] how do you know when to use self other than when it's clear you'll need to share the variable with other functions?

[Pythonite] when it's inside class definition see above: all defs are indendet from class person and class finnish so they 'belong' to class and share attributes so self is like dictionary, except: instead of self["foo"] you can do self.foo and when you do: obj.some_method(5) it's automatically translated into: FooClass.some_method(obj, 5) (definition for some_method was: def some_method(self, count): ) above holds for C++ too, except you donīt write "def" anywhere (you can, but it's not needed)

Class example In C++:

some_method(int count) { return age*count; }

And in C: int FooClass::some_method(int count)

Same Example in Python def some_method(self, count): return self.age * count

Another class tutorial:

phonebook={"Jaakko": "09-7857678", "Marja": "05-239778", "Elli": "02-287578", "Matti": "07-2197837"} print "Phone number for Matti is:",phonebook["Matti"] namelist=phonebook.keys() namelist.sort() print "Sorted phonebook:" for name in namelist: print "Name:",name,"Phonenumber:",phonebook[name]

Output for the above code is: Name: Elli Phonenumber: 02-287578 Name: Jaakko Phonenumber: 09-7857678 Name: Marja Phonenumber: 05-239778 Name: Matti Phonenumber: 07-2197837

[PythonNewbie] .sort and .keys are methods

[Pythonite] yup

[PythonNewbie] but are they classes as well?

[Pythonite] hmm.. they are built-in types... and you can't inherit from then but in some sense they are classes JPython they are really classes so it's an implementation issue mostly.

Send corrections or comments to Pato