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

What's In A Name? - 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


Let's now add a few properties to the mix, by modifying the class definition to support some additional characteristics:

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 def set_snake_name(self, name): # code goes here self.name = name
The class definition now contains one additional method, set_snake_name(), which modifies the value of the property "name". Let's see how this works:

>>> alpha = veryBigSnake() >>> beta = veryBigSnake() >>> >>> # name the alpha snake >>> alpha.set_snake_name("Peter Python") >>> alpha.name 'Peter Python' >>> >>> # name the beta snake >>> beta.set_snake_name("Bobby Boa") >>> beta.name 'Bobby Boa' >>> >>> # rename the alpha snake >>> alpha.set_snake_name("Sally Snake") >>> alpha.name 'Sally Snake' >>>
As the illustration above shows, once new objects are defined, their individual methods and properties can be accessed and modified independent of each other. This comes in very handy, as the next few pages will show.

It's also important to note the manner in which object methods and properties are accessed - by prefixing the method or property name with the name of the specific object instance.

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