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