Effects of changing attribute values

The effects of changing attribute values are as follows:

• If you change the class attribute first, the instance value also changes:

• If you change the instance attribute first, the class value does not change:

Warning If the attribute is a mutable data type, such as a list, and you use a method that changes the attribute in place (such as the append() method of lists—see Chapter 8), then changing the instance attribute does change the class attribute. Be sure to operate only on copies of mutable instance attributes or avoid using change-in-place methods on them.

• Giving a new value to the instance attribute overrides the value it originally gets from the class. So if you change the class attribute after you change the instance attribute, the instance attribute does not change.

Was this article helpful?

0 0

Post a comment