Concepts / Defining Your First Class

Defining Your First Class

Object construction using ClassName() creates an instance and assigns it to a variable for later access.

  • Programming

From Class to Object

A class gives you a named way to work with a kind of object. Object construction begins when you write the class name followed by parentheses, as in ClassName(). This creates an instance. Assigning that expression to a variable gives you a name through which you can access the new object later.

What do you think happens?

Suppose PartyAnimal() is used to construct an object and the result is assigned to a variable named an. What does an represent afterward?

  • The class definition itself

  • A newly created instance

  • Only the method named party

  • The self parameter by itself

Reveal answer
Answer:

A newly created instance

ClassName() creates an instance. The assignment gives that instance the variable name an, so later expressions can access the object through an.

Construction and Assignment

ClassName()construction expressionnew instanceobjectanvariable namecreatesassigned to
What happens when ClassName() creates an instance and that new object becomes linked to a variable?
Worked example

Naming a Constructed Object

Use the class name PartyAnimal to construct an object and make it available through the variable an.

  1. 1
    Construct

    Use PartyAnimal() as the construction expression. The class-name call creates an instance.

  2. 2
    Assign

    Associate the created instance with the variable an. The variable can now be used to access that specific object.

  3. 3
    Continue

    Expressions beginning with an can refer to data or methods belonging to this instance.

Result

The variable an provides access to the instance created from PartyAnimal().

The important distinction is between the class name and the instance variable. PartyAnimal is used as the class in the construction expression, while an is the variable used afterward to reach the particular instance that was created.

Finding Data with Dot Notation

Dot notation places an object before a dot and the requested attribute or method after it. The object before the dot tells Python which instance is involved. Therefore, object.attribute refers to data belonging to that specific object, while object.method() requests behavior through that same object.

anspecific instanceattributeobject datamethod()object behavioraccessescalls
How does object.attribute or object.method identify the object's data or behavior?
Worked example

Reading the Expression

Interpret the two parts of an.party().

  1. 1
    Before the dot

    an identifies the particular object whose behavior is being requested.

  2. 2
    After the dot

    party identifies the method associated with that object.

  3. 3
    Parentheses

    The parentheses indicate that the method is being called.

Result

an.party() calls the party method for the instance represented by an.

Connecting Methods with self

Inside a method, the self parameter refers to the object instance that called the method. This is what connects the method's work to one specific object's data. When a method uses self, it is working with the instance associated with the current call rather than with an unspecified object.

ancalling instancepartymethodselfthe instance anobject datadata reached through selfcallsreceiveslinks to
How does Python connect a method call to the specific object instance whose data the method should access?

For a call made through an, self refers to the instance represented by an. If the same method is called through a different object, self refers to that different calling instance instead. The method name can stay the same while self determines which object's data is connected to the method.

Worked example

Following self Through a Call

Explain what self represents when an.party() is called.

  1. 1
    Select the instance

    The name an identifies the object before the dot.

  2. 2
    Select the method

    The name party identifies the method after the dot.

  3. 3
    Connect the method

    Within that method call, self refers to the instance identified by an.

  4. 4
    Reach the instance data

    The method can use self to work with data belonging to that particular instance.

Result

In an.party(), self is the instance represented by an.

Two Ways to Call a Method

A method can be called with shorthand dot notation or with explicit class syntax. The shorthand form is an.party(). The explicit form is PartyAnimal.party(an). Both forms call the party method for the same object. In the explicit form, the object is written as an argument to the class's method reference; in the dot form, the object before the dot supplies that method context.

an.party()object dot methodpartysame methodPartyAnimal.party(an)class method objectself = ansame instance contextcallscallsobject becomespasses
Why are object.method() and Class.method(object) equivalent, and how does the object become the method's self argument?
Calling formObject shownMethod selectedself refers to
an.party()an before the dotparty after the dotthe instance represented by an
PartyAnimal.party(an)an inside the parenthesesparty after the class namethe instance represented by an

The two forms use different notation but connect the same method to the same instance.

Mistakes with Instance Context

  • Common mistake

    Treating ClassName() as the object variable

    What it looks like

    After construction, confusing PartyAnimal with an

    Why it fails

    PartyAnimal is the class name used in the construction expression, while an is the variable assigned to the newly created instance.

    Do this instead

    Track the sequence: ClassName() creates an instance, and assignment gives that instance a variable name.

  • Common mistake

    Ignoring the object before the dot

    What it looks like

    Reading an.party() as if party were selected without reference to an

    Why it fails

    Dot notation accesses data or methods belonging to the specific object placed before the dot.

    Do this instead

    Identify the object first, then identify the attribute or method after the dot.

  • Common mistake

    Treating self as an unrelated, general object

    What it looks like

    Assuming self always refers to one fixed instance

    Why it fails

    self refers to the object instance that called the method.

    Do this instead

    For each call, ask which object supplied the method call. That object is the instance linked through self.

  • Common mistake

    Assuming the two method-call forms select different behavior

    What it looks like
    Treating an.party() and PartyAnimal.party(an) as unrelated calls
    Why it fails

    The source identifies both forms as equivalent.

    Do this instead

    Compare the selected method and the instance supplied to it. Both use party with the instance represented by an.

Practice the Trace

MEDIUM

A class named MusicPlayer is used to construct an object assigned to the variable player. Explain what player represents, what player.volume refers to, what player.play() requests, and what self represents while play is being called through player.

Hints
  • Start with the effect of ClassName() followed by assignment.
  • Use the name before the dot to identify the instance.
  • For self, identify the object that made the method call.
  • You can describe the relationship without writing a complete class definition.
Worked example

Checking the Answer

Explain the object and method relationships in the MusicPlayer example.

  1. 1
    player

    player is the variable assigned to the instance created by MusicPlayer().

  2. 2
    player.volume

    The expression uses dot notation to access volume as data belonging to the instance represented by player.

  3. 3
    player.play()

    The expression uses dot notation to call play for the instance represented by player.

  4. 4
    self

    During that call, self refers to the instance represented by player.

Result

The variable identifies the instance, dot notation selects its data or method, and self links the method to that same instance.

Key Takeaways

  1. 1
    ClassName() creates an instance, and assignment gives that instance a variable for later access.
  2. 2
    Dot notation uses the object before the dot to select that object's data or method.
  3. 3
    self refers to the instance that called the method, linking the method to that instance's data.
  4. 4
    object.method() and Class.method(object) are equivalent ways to call a method for the same object.
  5. 5
    When tracing an object-oriented expression, identify the instance first, then the requested attribute or method.

At a glance

  • Use ClassName() to construct an instance and assign it to a variable.
  • Use dot notation to access data or methods belonging to a specific instance.
  • The self parameter connects a method to the instance that called it.
  • The shorthand and explicit method-call forms are equivalent when they use the same method and object.

Related concepts

More concepts starting with D