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.
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,
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()
>>>