[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
BeginnersGuideToPython_Classes
Displaying differences between revision 1 and the latest revision


== Beginner's Guide To Python, Classes ==

[anchor:ClassBasics]
=== Class Basics ===
One of the most useful and powerful aspects of Python programming is [italic]classes[/italic]. 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.¶

[anchor:TheClassStatement]
=== The class Statement ===
As with functions, classes in Python are created by an executable statement. To begin using Python's object oriented features[sup]1[/sup], you simply need to issue a class statement:¶

[code]¶
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¶

>>>¶
[/code]¶

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:¶

[code]¶
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>¶
>>> ¶
[/code]¶

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

[anchor:Members]
=== Class Data Members ===

[anchor:Methods]
=== Class Methods ===

[anchor:Inheritance]
=== Class Inheritance ===

[hr]¶
(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: 10194, Current Rev: 2 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  
© 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. Site Management by Lars Hagelin at Kontantkort.se.