Welcome to devshed, a community dedicated to spreading the the spirit of open source to beginner and Python Big Data Company Gets DARPA Funding. dev shed. Teaching open source technology one tutorial at a time! to find informative threads on programming in PHP, Perl, Java, Python, C, Ruby and more. “[If] I could only own one Python book, it would be Core Python Programming by Wesley —ptonman, Dev Shed Forums “Python is simply a beautiful language.

Author: Arashitilar Kagore
Country: Maldives
Language: English (Spanish)
Genre: Love
Published (Last): 12 July 2005
Pages: 31
PDF File Size: 10.97 Mb
ePub File Size: 7.81 Mb
ISBN: 980-6-43161-141-2
Downloads: 90157
Price: Free* [*Free Regsitration Required]
Uploader: Dom

As a true object-oriented language, Python is a great place to start learning about OO programming. In this first segment of a two-part article, take your Python skills to the next level with a discussion of classes and class instances.

Object Oriented Programming With Python (part 1) – devshed

Practical and not-so-practical examples included. 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.

A typical class definition contains both variables and functions, and serves as the template from which to spawn specific instances of that class.

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.

This comes in very handy, as the next few pages will show. All class variables and functions are indented within this block, and are written as fevshed would normally code them. In order to create a new instance of a class, you need to simply create a new variable referencing the class.

You can now access all the methods and properties of the class via this variable. When a class method is called, it requires to be passed a reference to the instance that is calling it.

Python Archives – devshed

With that in mind, consider this: Take me home today! This makes it possible to simplify the class definition above to: It is generally not advisable to do this, as pythonn would violate the integrity of the object; the preferred technique is always to use the methods exposed by the object to change object properties.


By limiting yourself to exposed methods, you are provided with a level of protection which ensures that changes in pytthon class code do not have repercussions on your code. This next package allows you to create different Clock objects, and initialize each one to a specific time zone.

You could create a clock which displays local time, another which displays the time in New York, and a third which displays the time in London — all through the power of Python objects. GMTTime[3] calculate time if self. Once these attributes have been set, the display method takes over and performs some simple calculations to obtain the local time in that city.

Examples are illustrative only, and are not meant for a production environment.