Online Book Reader

Home Category

Learning Python - Mark Lutz [376]

By Root 1606 0
top to bottom. Assignments that happen during this process create names in the class’s local scope, which become attributes in the associated class object. Because of this, classes resemble both modules and functions:

Like functions, class statements are local scopes where names created by nested assignments live.

Like names in a module, names assigned in a class statement become attributes in a class object.

The main distinction for classes is that their namespaces are also the basis of inheritance in Python; reference attributes that are not found in a class or instance object are fetched from other classes.

Because class is a compound statement, any sort of statement can be nested inside its body—print, =, if, def, and so on. All the statements inside the class statement run when the class statement itself runs (not when the class is later called to make an instance). Assigning names inside the class statement makes class attributes, and nested defs make class methods, but other assignments make attributes, too.

For example, assignments of simple nonfunction objects to class attributes produce data attributes, shared by all instances:

>>> class SharedData:

... spam = 42 # Generates a class data attribute

...

>>> x = SharedData() # Make two instances

>>> y = SharedData()

>>> x.spam, y.spam # They inherit and share 'spam'

(42, 42)

Here, because the name spam is assigned at the top level of a class statement, it is attached to the class and so will be shared by all instances. We can change it by going through the class name, and we can refer to it through either instances or the class.[63]

>>> SharedData.spam = 99

>>> x.spam, y.spam, SharedData.spam

(99, 99, 99)

Such class attributes can be used to manage information that spans all the instances—a counter of the number of instances generated, for example (we’ll expand on this idea by example in Chapter 31). Now, watch what happens if we assign the name spam through an instance instead of the class:

>>> x.spam = 88

>>> x.spam, y.spam, SharedData.spam

(88, 99, 99)

Assignments to instance attributes create or change the names in the instance, rather than in the shared class. More generally, inheritance searches occur only on attribute references, not on assignment: assigning to an object’s attribute always changes that object, and no other.[64] For example, y.spam is looked up in the class by inheritance, but the assignment to x.spam attaches a name to x itself.

Here’s a more comprehensive example of this behavior that stores the same name in two places. Suppose we run the following class:

class MixedNames: # Define class

data = 'spam' # Assign class attr

def __init__(self, value): # Assign method name

self.data = value # Assign instance attr

def display(self):

print(self.data, MixedNames.data) # Instance attr, class attr

This class contains two defs, which bind class attributes to method functions. It also contains an = assignment statement; because this assignment assigns the name data inside the class, it lives in the class’s local scope and becomes an attribute of the class object. Like all class attributes, this data is inherited and shared by all instances of the class that don’t have data attributes of their own.

When we make instances of this class, the name data is attached to those instances by the assignment to self.data in the constructor method:

>>> x = MixedNames(1) # Make two instance objects

>>> y = MixedNames(2) # Each has its own data

>>> x.display(); y.display() # self.data differs, MixedNames.data is the same

1 spam

2 spam

The net result is that data lives in two places: in the instance objects (created by the self.data assignment in __init__), and in the class from which they inherit names (created by the data assignment in the class). The class’s display method prints both versions, by first qualifying the self instance, and then the class.

By using these techniques to store attributes in different objects, we determine their scope of visibility. When attached to classes, names are shared; in instances,

Return Main Page Previous Page Next Page

®Online Book Reader