Defining Classes and Creating Instances
The class keyword creates a new type in Python, not just a container for code.
From Definition to Object
Python does not treat a class as merely a container for related code. When you define a class, the class keyword creates a new type. When you later call that class, Python creates an instance, which is an object belonging to that new type.
The PartyAnimal Type
Consider a class named PartyAnimal. Its definition describes an __init__ method that sets up an attribute named x and a party method that increments x and prints a message. The important point is not only what these members do. The definition also introduces PartyAnimal as a new type that Python can recognize.
In this example, PartyAnimal is the custom type and an is an instance of that type. Calling PartyAnimal() creates the instance. The instance has the x attribute and the party method described by the class.
What do you think happens?
What do you expect type(an) to identify?
Reveal answer
Answer: PartyAnimal
The object an was created by calling PartyAnimal(), so its type is the custom type PartyAnimal rather than a built-in type such as int or str.
Built-In and Custom Types
| Object | Type | How the type is provided |
|---|---|---|
| 5 | int | Built in to Python |
| 'Alice' | str | Built in to Python |
| an | PartyAnimal | Created with the class keyword |
Python provides built-in types such as int and str, but it also lets you introduce your own types. An instance of PartyAnimal is therefore comparable at the type level to the integer object 5: both are objects, and each belongs to a recognized type. Their types are different because one is int and the other is PartyAnimal.
Reading an Object with type()
The type() function returns the class of an object. It can identify the type of a built-in object such as 5 or 'Alice', and it can also identify the type of an instance created from a custom class.
class PartyAnimal: def __init__(self): self.x = 0 def party(self): self.x = self.x + 1 an = PartyAnimal() print(type(5)) print(type(an)) print(type(an.x)) print(type(an.party))
Exploring Members with dir()
The dir() function lists the attributes and methods available on an object. For an instance such as an, the list includes members defined for the custom class, such as x and party, along with special methods that Python provides to the class.
The result of dir(an) is a list of names, not a description of what each member does. In the PartyAnimal example, finding x confirms that the instance has the attribute established by __init__. Finding party confirms that the instance exposes the method defined by the class. The same listing also contains special methods automatically associated with the class.
Separate Instances
A class can create more than one instance. If you create an2 with PartyAnimal(), both an and an2 have type PartyAnimal, and both have an x attribute and a party method. They are nevertheless separate objects with separate data. The class identifies what kind of objects they are; each instance is an individual object of that type.
Two Instances of One Custom Type
Track the relationship between PartyAnimal, an, and an2.
Define the type: The class definition creates the custom type PartyAnimal.
Create the first instance: Calling PartyAnimal() creates an, an object whose type is PartyAnimal.
Create the second instance: Calling PartyAnimal() again creates an2, another object whose type is PartyAnimal.
Compare their data: Both instances have an x attribute and a party method, but they are separate objects with separate data.
an and an2 share the same custom type, PartyAnimal, while remaining distinct instances.
Common Inspection Mistakes
Treating a class as only a code container
The class keyword creates a new type that Python recognizes.
Fix:
Describe PartyAnimal as a custom type and an as an instance of that type.Expecting type(an) to return a built-in type
an was created by calling PartyAnimal(), so its type is PartyAnimal.
Fix:
Use type(an) to identify the custom class used to create the instance.Assuming dir() only lists members written by the programmer
dir() also lists special methods that Python provides on the object.
Fix:
Treat the result as a complete listing of available attributes and methods, including special methods.Confusing an instance with one of its attributes
an is a PartyAnimal instance, while an.x is an integer attribute.
Fix:
Apply type() to the specific object whose type you want to inspect.
Check Your Understanding
Suppose a class named Book is defined and then an instance named first_book is created by calling Book(). Predict the result of type(first_book). Then describe what you would look for in dir(first_book) to confirm that the instance exposes a class-defined attribute or method.
Hints
- The type is determined by the class used to create the instance.
- dir() lists names available on the object, including user-defined attributes and methods.
- Do not expect dir() to contain only names written in the class definition; special methods can also appear.
- A class definition introduces a new type. Calling the class creates an instance of that type. The type() function identifies the class of an object, while dir() lists the attributes and methods available on it. Custom-class instances are genuine Python objects with their own custom type, just as 5 is an object of type int.
Key Takeaways
- The class keyword creates a new custom type in Python.
- Calling a class creates an instance of that type.
- type() reports the class of an object, including an instance of a custom class.
- dir() lists the attributes and methods available on an object, including special methods.
- Multiple instances can share one custom type while remaining separate objects with separate data.