Home arrow Python arrow Page 2 - Object Oriented Programming With Python (part 1)

A Very Big Snake - Python

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


A class definition typically looks like this:

class veryBigSnake: # some useful methods def eat(self): # code goes here def sleep(self): # code goes here def squeeze_to_death(self): # code goes here
Once the class has been defined, you can instantiate a new object by assigning it to a Python variable,

Python 1.5.2 (#1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66 19990314/Linux (egcs- on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> python = veryBigSnake() >>>
which can then be used to access all object methods and properties.

>>> python.eat() >>> >>> python.sleep() >>>
As stated above, each instance of a class is independent of the others - which means that, if I was a snake fancier, I could create more than one instance of veryBigSnake(), and call the methods of each instance independently.

>>> alpha = veryBigSnake() >>> beta = veryBigSnake() >>> # make the alpha snake eat >>> alpha.eat() >>> >>> # make the beta snake eat and sleep >>> beta.eat() >>> >>> beta.sleep() >>>

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