Understanding self and Instance Attributes
The class keyword creates a new type in Python, not just a container for code.
A New Category of Object
Python's class keyword does more than organize related code. It creates a new type. Python already provides built-in types such as int and str, but a class lets you introduce another category of object that Python recognizes as its own type.
When you create an instance from a custom class, that instance is an object of the custom type. It is not automatically an int or a str, even if one of its attributes contains an integer or a string.
Following self to an Instance
An instance attribute is data associated with one particular object. In the PartyAnimal example described in the source, each instance has an x attribute, and the party method increments that instance's x value. The name self identifies the particular instance involved when the method works with that attribute. Therefore, a method call on an points to an's x, while a method call on a second instance points to that second instance's x.
class PartyAnimal: def __init__(self): self.x = 0 def party(self): self.x = self.x + 1 print("party", self.x) an = PartyAnimal() an2 = PartyAnimal() an.party() an2.party()
Reading an Object's Type
The type() function reports the class of an object. For a built-in value such as 5, type() identifies int. For a string such as 'Alice', it identifies str. For the instance an created from PartyAnimal, it identifies PartyAnimal. This confirms that the class is a real type recognized by Python.
<class 'int'>
<class 'str'>
<class '__main__.PartyAnimal'>
<class 'int'>The type of an object and the types of its attributes are separate questions. In this example, an has type PartyAnimal, while an.x has type int.
Inspecting Attributes with dir
The dir() function lists the attributes and methods available on an object. For a PartyAnimal instance, the listing includes the user-defined x attribute and party method. It also includes special methods that Python automatically provides to every class. The complete listing can be long, so the important skill is to use dir() as an inspection tool: look for the names your class defines, while remembering that the object also has Python-provided members.
True
TrueMistakes Beginners Make
Treating a class as only a container for code
The class keyword creates a new type, and instances created from it are objects of that type.
Fix:
Use type(an) to confirm that the instance has type PartyAnimal.Assuming an instance has the type of one of its attributes
The instance and its attribute are different objects and can have different types.
Fix:
Check them separately with type(an) and type(an.x).Assuming two instances share the same instance data
Separate instances have separate data. The source describes each PartyAnimal instance as having its own x attribute.
Fix:
Track the selected instance through self when a method reads or changes x.Expecting dir() to show only names written by the programmer
dir() lists user-defined attributes and methods as well as special methods Python automatically provides.
Fix:
Use dir() as a complete inventory of available names, not as a copy of only your class body.
Practice the Inspection Tools
Class, Instance, and Attribute
A PartyAnimal instance is stored in an. What should you expect from type(an), type(an.x), and dir(an)?
Identify the instance type: Because PartyAnimal is a class and an is created from it, type(an) reports PartyAnimal.
Identify the attribute type: The x attribute is initialized as an integer, so type(an.x) reports int.
Inspect available names: dir(an) includes x and party, along with special methods supplied by Python.
The instance has type PartyAnimal, its x attribute has type int, and dir(an) reveals the names available through the instance.
Suppose you create two PartyAnimal instances named first and second. Explain why both can have type PartyAnimal while still having separate x attributes. Then describe what dir(first) should help you find.
Hints
- Separate the question about the class of an object from the question about the data stored on that object.
- Remember that each instance has its own x attribute.
- Look for both the user-defined names and the special methods in dir(first).
The Working Mental Model
- The class keyword creates a new custom type, not merely a container for code.
- An instance created from a class is an object of that class's type, just as 5 is an object of type int.
- self identifies the particular instance whose attributes a method reads or changes.
- type(object) reports the object's class, while type(object.attribute) reports the attribute's class.
- dir(object) lists available attributes and methods, including user-defined names and Python-provided special methods.
Key Takeaways
- A Python class defines a new type that Python can recognize.
- Instances of the same class share the class type but remain separate objects with separate instance data.
- self connects a method's attribute operations to the particular instance involved in the method call.
- Use type() to identify an object's class and dir() to inspect its available attributes and methods.
- A custom instance can have one type while its attributes have other types.