[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
BeginnersGuideToPython_Classes
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:
Here we have created an object named mc which is an instance of MyClass.
(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.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
