Concepts / Methods and How They Access Object State

Methods and How They Access Object State

The class keyword creates a new type in Python, not just a container for code.

  • Programming

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.

includesincludescreatesPython objectsBuilt-in typesint, str, listaninstance of PartyAnimalPartyAnimalcustom type
What does the class definition create, and what is produced when that class is called?

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.

storesprovidesincrementsanPartyAnimal instancexinteger attributexincremented valuepartymethod
How does the party method use the state stored on the particular object an?

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?

Inspect an: Because an was created from PartyAnimal, type(an) reports the custom class PartyAnimal.

Inspect an.x: The x attribute contains an integer, so type(an.x) reports int.

Inspect 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.

type()type()5valueintreported typeanPartyAnimal instancePartyAnimalreported type
What does type() report for a built-in value compared with an instance of a custom class?

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 toolQuestion it answersSource 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.xWhat type is this attribute value?int
type() on an.partyWhat 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.

createscreatesstoresstoresPartyAnimalcustom typeanx and partyxan's integer valuean2x and partyxan2's integer value
How can two instances have the same methods while holding separate attribute values?

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

  • 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: Think of PartyAnimal as a custom type that defines a category of object.

  • Expecting type(an) to report the type of one of an's attributes.

    type(an) inspects the instance itself. The integer type belongs to an.x.

    Fix: Use type(an) for the instance and type(an.x) for the attribute value.

  • Assuming dir() shows only names written by the programmer.

    dir() includes user-defined names and special methods that Python automatically adds to every class.

    Fix: Use dir() as a complete inspection list, then distinguish your names from automatically supplied special methods.

  • Assuming two instances share one set of attribute data.

    They have the same type but are separate objects with separate data.

    Fix: Track each instance's state separately.

Check Your Understanding

MEDIUM

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, while dir() lists available names.
  • Compare the class of an and an2 with the attribute data stored on each instance.
  1. A 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 and dir() 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.