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

Digging Deep - 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


Now that you've got the concepts straight, let's take a look at the nitty-gritty of a class definition.

class veryBigSnake: # method and property definitions
Every class definition begins with the keyword "class", followed by a class name. You can give your class any name that strikes your fancy, so long as it doesn't collide with a reserved word. All class variables and functions are indented within this block, and are written as you 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.

>>> alpha = veryBigSnake() >>>
In English, the above would mean "create a new object of class veryBigSnake and assign it to the variable 'alpha'".

You can now access all the methods and properties of the class via this variable.

>>> # accessing a method >>> alpha.set_snake_name("Peter Python") >>> >>> # accessing a property >>> alpha.name >>>
Again, in English,

>>> alpha.set_snake_name("Peter Python") >>>
would mean

"execute the method set_snake_name() with parameter 'Peter Python' of this specific instance of the class veryBigSnake".

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