Object Oriented Programming With Python (part 1) - A Very Big Snake
(Page 2 of 6 )
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()
>>>
Next: What's In A Name? >>
More Python Articles
More By icarus, (c) Melonfire