[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
BeginnersGuideToPython_Classes

Beginner's Guide To Python, Classes

Class Basics

One of the most useful and powerful aspects of Python programming is classes. Classes are the foundation of Object Oriented Programming. A class allows you to encapsulate data with methods that operate on that data into a single special type.

The class Statement

As with functions, classes in Python are created by an executable statement. To begin using Python's object oriented features1, you simply need to issue a class statement:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> class MyClass:
	pass
>>>


In languages such as C++ or Java, classes are just blueprints for creating instances of that class. In Python, the class statement actually creates a callable object which returns instances of that class. This statement creates such an object factory named MyClass. To create an instance of MyClass, simply use it like a function:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> class MyClass:
	pass
>>> mc = MyClass()
>>> mc
<__main__.MyClass instance at 0x00904D00>
>>> 


Here we have created an object named mc which is an instance of MyClass.

Class Data Members

Class Methods

Class Inheritance


(1) You have actually been using Python's object oriented nature implicitly already. Here, however, we begin using it explicitly by creating our own objects.

last edited (February 5, 2003) by infidel, Number of views: 5011, Current Rev: 2 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.