HomePython Object Oriented Programming With Python (part 1)
Object Oriented Programming With Python (part 1)
As a true object-oriented language, Python is a great place tostart learning about OO programming. In this first segment of a two-partarticle, take your Python skills to the next level with a discussion ofclasses and class instances. Practical (and not-so-practical) examplesincluded.
You're probably tired of hearing me say it, but indulge me one more time - when it comes to object-oriented programming, very few languages have Python's capabilities.
This is primarily because Python was built from the ground up as an object-oriented language, and so provides built-in constructs that make it simple for developers to structure code for maximum reusability. Python's "dynamic typing", which automatically recognizes objects like numbers, strings and lists, and negates the need to declare variable types and sizes, offers an advantage not found in languages like C or Java, while automatic memory allocation and management, together with a vast array of pluggable libraries and high-level abstractions, complete the picture.
Over the course of this two-part article, I'm going to take an in-depth look at Python's OO capabilities, together with examples and explanations to demonstrate just how powerful it really is. I'll be covering most of the basics - classes, objects, attributes and methods - and a couple of the advanced concepts - constructors, destructors and inheritance. And if you're new to object-oriented programming, or just apprehensive about what lies ahead, don't worry - I promise this will be a lot easier than you think.
Let's get going!{mospagebreak title=The Basics} Before we begin, let's just go over the basics quickly:
In Python, a "class" is simply a set of program statements which perform a specific task. A typical class definition contains both variables and functions, and serves as the template from which to spawn specific instances of that class.
These specific instances of a class are referred to as "objects". Every object has certain characteristics, or "properties", and certain pre-defined functions, or "methods". These properties and methods of the object correspond directly with the variables and functions within the class definition.
Once a class has been defined, Python allows you to spawn as many instances of the class as you like. Each of these instances is a completely independent object, with its own properties and methods, and can thus be manipulated independently of other objects. This comes in handy in situations where you need to spawn more than one instance of an object - for example, two simultaneous database links for two simultaneous queries, or two shopping carts.
Classes also help you keep your code modular - you can define a class in a separate file, and include that file only in the pages where you plan to use the class - and simplify code changes, since you only need to edit a single file to add new functionality to all your spawned objects.