O'Reilly logo

Ubuntu Unleashed 2013 Edition: Covering 12.10 and 13.04, Eighth Edition by Matthew Helmke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Class and Object Variables

Each object has its own set of functions and variables, and you can manipulate those variables independently of objects of the same type. In addition, some class variables are set to a default value for all classes and can be manipulated globally.

This script demonstrates two objects of the dog class being created, each with its own name:

class Dog(object):     name = "Lassie"     def bark(self):         print self.name + " says 'Woof!'"     def set_name(self, name):         self.name = namefluffy = Dog()fluffy.bark()poppy = Dog()poppy.set_name("Poppy")poppy.bark()

That outputs the following:

Lassie says 'Woof!'Poppy says 'Woof!'

There, each dog starts with the name Lassie, but it gets customized. Keep in mind that ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required