Methods and How They Access Object State
A method is available on an instance, and the source example shows that the party method changes the instance's x attribute. This is object state: information stored as attributes on that object.
A New Category of Object
When you write x = 5, x refers to an integer, whose type is int. When you write name = 'Alice', name refers to a string, whose type is str. Python also lets you define your own types with the class keyword. A class is therefore more than a place to organize related code: it introduces a new category of object that Python recognizes.
Following State Through a Method
Consider the source example class PartyAnimal. Its initialization method sets up an attribute named x. Its party method increments x and prints a message. After an instance named an is created, calling its party method works with the state stored on that particular object. The important relationship is between the method and the instance: party changes an's x, not an unrelated integer or another object's attribute.
A method is available on an instance, and the source example shows that the party method changes the instance's x attribute. This is object state: information stored as attributes on that object.
Reading an Object's Type
The type() function reports the class of an object. For built-in values, it can report types such as int or str. For the source example, type(an) reports PartyAnimal because an is an instance created from that custom class. This is the central point: PartyAnimal is a genuine type, just as int is a genuine type.
Interpreting Three Types
Suppose an is an instance of PartyAnimal, an.x is the integer attribute created during initialization, and an.party refers to the party method. What types should type() report for these three objects or values?
- 1Inspect an
Because an was created from PartyAnimal,
type(an)reports the custom class PartyAnimal. - 2Inspect an.x
The x attribute contains an integer, so
type(an.x)reports int. - 3Inspect an.party
The party attribute refers to a method, so its type is method.
The three observations distinguish the instance itself, its integer attribute, and its method: PartyAnimal, int, and method.
Inspecting Names with dir
The dir() function lists the attributes and methods available on an object. For the PartyAnimal instance an, the list includes x and party. It also includes special methods that Python automatically provides to every class. Therefore, dir() is an inspection tool: it helps you discover what names are available, while type() tells you the object's class.
| Inspection tool | Question it answers | Source example |
|---|---|---|
| type() | What class is this object? | type(an) reports PartyAnimal |
| dir() | What attributes and methods are available? | The list includes x and party |
| type() on an.x | What type is this attribute value? | int |
| type() on an.party | What kind of available member is this? | method |
Use type() to identify a type and dir() to inspect available names.
Separate State, Shared Type
Create a second instance named an2 from PartyAnimal. Both an and an2 have type PartyAnimal. Both provide the x attribute and the party method. However, they are separate objects with separate data. A change made through the method on one instance concerns that instance's stored state, not the other instance's state.
A class determines the type and available behavior. Each instance has its own object state. Same class does not mean same object or shared attribute data.
Mistakes in Object Inspection
Common mistake Treating a class as only a container for code.
What it looks likeThinking of PartyAnimal only as a place where party is stored.
Why it failsThe class keyword creates a new type, and instances created from it are objects of that type.
Do this insteadThink of PartyAnimal as a custom type that defines a category of object.
Common mistake Expecting type(an) to report the type of one of an's attributes.
What it looks likeExpecting type(an) to report int because an has an integer x attribute.
Why it failstype(an)inspects the instance itself. The integer type belongs toan.x.Do this insteadUse type(an) for the instance and type(an.x) for the attribute value.
Common mistake Assuming dir() shows only names written by the programmer.
What it looks likeTreating every name in dir(an) as a user-defined attribute or method.
Why it failsdir()includes user-defined names and special methods that Python automatically adds to every class.Do this insteadUse dir() as a complete inspection list, then distinguish your names from automatically supplied special methods.
Common mistake Assuming two instances share one set of attribute data.
What it looks likeAssuming an and an2 refer to the same x value because both come from PartyAnimal.
Why it failsThey have the same type but are separate objects with separate data.
Do this insteadTrack each instance's state separately.
Check Your Understanding
A class named PartyAnimal creates an instance named an. The instance has an integer attribute x and a method named party. Explain what type(an), type(an.x), type(an.party), and dir(an) tell you. Then explain what remains the same and what is separate if a second instance named an2 is created.
Hints
- First distinguish the instance from the values available through it.
- Remember that
type()reports one object's class, whiledir()lists available names. - Compare the class of an and an2 with the attribute data stored on each instance.
- 1A class definition creates a new Python type. Calling that class creates an instance of the type. Methods can work with the attributes stored on the particular instance, such as party incrementing x on an. Use
type()to identify an object's class anddir()to inspect its available attributes and methods. Two instances can share the same custom type and methods while keeping separate object state.
Key Takeaways
- The class keyword creates a new, recognized Python type.
- An instance created from a custom class is an object of that custom type.
- A method can operate on the attributes stored on its particular instance.
type()reports an object's class, including a custom class such as PartyAnimal.dir()lists available attributes and methods, including automatically supplied special methods.