Concepts / Instance Attributes and Methods

Instance Attributes and Methods

An attribute represents data associated with the object, while a method represents behavior that can be used with the object.

  • Programming

From Object to Behavior

An object is a constructed instance of a class. It contains the attributes and methods defined by that class. An attribute represents data associated with the object, while a method represents behavior that can be used with the object. Instance attributes and methods work together: the attributes hold information for one object, and the methods can access that object's information.

Definition

Object: A constructed instance of a class. An object contains the attributes and methods defined by the class. The terms object and instance are sometimes used interchangeably.

objectconstructed instanceattributesobject datamethodsobject behaviorcontainscontains
What belongs to a constructed object?

Following self

Instance methods have a special first parameter that is conventionally named self. This parameter gives the method access to the particular object instance involved in the call. Through self, the method can reach attributes on that object and can call methods associated with that object.

object instancethe current objectselfreference to the instanceinstance attributedata belonging to theobjectinstance methodbehavior for the objectsupplies current instanceaccessescan call
How does self connect a method to the specific object whose attributes it should access or change?
Worked example

Tracing one object's method

Imagine an object with an instance attribute named score and an instance method named add_point. When add_point runs, which object should self identify?

  1. 1
    Identify the object

    The method is being used with one particular object instance.

  2. 2
    Identify self

    The conventional first parameter self refers to that particular object instance.

  3. 3
    Reach the attribute

    Because self gives access to the object, the method can use the object's score attribute.

  4. 4
    Change the object's data

    The method can use self to set or update an attribute belonging to that same object.

Result

self connects the running method to the specific object whose attributes and methods it should use.

Reading and Changing State

An instance attribute belongs to an individual object instance. An instance method can use self to access that attribute, read its current value, or set a new value. This is the central relationship between attributes and methods: the object's data is stored with the object, and the object's methods use the current instance to work with that data.

object instanceselfattributecurrent datamethodreads datamethodsets datacurrent instancereachesreadssets
How do an object's methods use self to read or update attributes stored on that same object?

Using Dot Notation

Dot notation is the way an object is used to reach its attributes and methods. The object appears first, followed by a dot and the attribute or method name. When dot notation is used to call an instance method, that call identifies the particular object instance for the method. Inside the method, the conventional self parameter provides access to that instance.

callerobjectmethodselfcurrent objectselectscallsreceives instance as
How does writing object.method() cause the method to run with that particular object supplied as self?
Worked example

Separating attribute access from method access

Consider an object named fan with an attribute named team and a method named cheer. What does each dot-notation use target?

  1. 1
    Attribute access

    The form fan.team uses the object fan to reach its team attribute.

  2. 2
    Method access

    The form fan.cheer() uses the object fan to reach and call its cheer method.

  3. 3
    Instance connection

    When the method runs, self identifies fan as the current object instance.

Result

The object before the dot determines which instance's attribute or method is being used.

Mistakes with Instance Members

  • Common mistake

    Treating self as an ordinary unrelated parameter.

    What it looks like

    A method uses self but the learner does not connect it to the object that made the call.

    Why it fails

    The first parameter is special because it gives the method access to the object instance involved in the operation.

    Do this instead

    When tracing a method call, identify the object before the dot and treat that object as the instance available through self.

  • Common mistake

    Confusing an object's attribute with an object's method.

    What it looks like

    A learner describes the object's stored data and its behavior as if they were the same kind of member.

    Why it fails

    Objects contain both attributes and methods, but attributes represent object data while methods provide behavior.

    Do this instead

    Ask whether the name identifies data associated with the object or behavior that can be used with the object.

  • Common mistake

    Ignoring the object before the dot.

    What it looks like

    A learner focuses on a method name but does not track which object is using it.

    Why it fails

    Dot notation uses the object to reach its attributes and methods, and the method's self parameter identifies that particular instance.

    Do this instead

    Read a dot-notation expression from left to right: identify the object, then identify the attribute or method reached through it.

Best practice

When explaining an instance method, always name the current object explicitly. A reliable explanation follows this pattern: the object is selected, the method is called through dot notation, self identifies that object inside the method, and the method uses self to access or set the object's attributes.

Practice the Trace

EASY

An object named player has an instance attribute named points and an instance method named earn. Trace the relationship in words: Which object does self identify when player uses earn, and what can the method reach through self?

Hints
  • Start with the object written before the dot.
  • Remember the conventional role of the first parameter in an instance method.
  • The method can use self to access attributes and call methods belonging to that object.

What do you think happens?

If the object named player calls its earn method, which object does self identify inside that method?

  • The player object

  • The method name

  • A separate unrelated object

Reveal answer
Answer:

The player object

The first parameter conventionally named self gives the method access to the particular object instance involved in the call.

Key Takeaways

  1. 1
    An object is a constructed instance of a class and contains attributes and methods defined by that class.
  2. 2
    Instance attributes store data associated with an object instance.
  3. 3
    Instance methods provide behavior that can work with the object's data.
  4. 4
    The first parameter of an instance method is conventionally named self and gives access to the current object instance.
  5. 5
    Dot notation uses an object to reach its attributes and methods; when a method is called, that object is the instance available through self.

At a glance

  • Objects combine attributes, which represent data, with methods, which represent behavior.
  • The conventional self parameter connects an instance method to the particular object involved in a call.
  • Methods use self to access or set attributes and to call methods belonging to the same object.
  • Dot notation identifies the object first and then reaches one of that object's attributes or methods.

Related concepts

More concepts starting with I