加载中...

Python

  1. Home
  2. Computing & Technology
  3. Python

"Hello, World!": A Quick Tutorial on Python

By Al Lukaszewski, About.com

3 of 6

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 method is mandatory if you want the object to know anything about itself (instead of being a brainless mass of functions and variables; the class must have a way of referring to itself. The second method simply adds the value of word to the Felicitations object. Finally, this class has the ability to print itself via a method called 'printme'.

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

More from About.com

New Year, New You

Take action today to accomplish your biggest goals with these motivating ideas.

Best Moves in a Bad Economy

Stay on top in this tough economy with our smart, easy-to-follow financial tips.

Python

  1. Home
  2. Computing & Technology
  3. Python
  4. Beginning Python
  5. Python "Hello World" Tutorial - A Python Class Called Felicitations

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

All rights reserved.