1. Home
  2. Computing & Technology
  3. Python

"Merry Christmas!": A Quick Tour of Python

From Al Lukaszewski, for About.com

3 of 7

A Class Called Felicitations

From this, we can create a class called Felicitations:

class Felicitations(object):
def __init__(self):
self.felicitations = [ ]
def addon(self, word):
self.felicitations.append(word)
def printme(self):
greeting = string.join(self.felicitations[0:], "")
print greeting

The class is based off of another type of object called 'object'. The first function is mandatory; the class must have a way of referring to itself. The second method simply adds the value of variable word to the Felicitations object. Finally, this class has the ability to print itself via a method called printme. This final method joins the various fields of the object, separating them by no space, and prints them.

In Python, indentation is important. Every nested block of commands must be indented the same amount. Python has no other way to differentiate between nested and non-nested blocks of commands.

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.