Home arrow Python arrow 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.

  1. Object Oriented Programming With Python (part 1)
  2. A Very Big Snake
  3. What's In A Name?
  4. Digging Deep
  5. Self-Involved
  6. Under Construction
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 44
November 01, 2001

print this article


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.

>>> More Python Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: