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

Get Ubuntu Unleashed 2014 Edition: Covering 13.10 and 14.04,Ninth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.