Concepts / Instance Attributes and Methods

Instance Attributes and Methods

The other method named party . The methods all have a special first parameter that we name by convention self . The first parameter gives us access to the object instance so we can set attributes and call methods using dot notation.

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

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.

containscontainsobjectconstructed instanceattributesobject datamethodsobject behavior
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.

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

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?

Identify the object: The method is being used with one particular object instance.

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

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

Change the object's data: The method can use self to set or update an attribute belonging to that same object.

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.

current instancereachesreadssetsobject instanceselfattributecurrent datamethodreads datamethodsets data
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.

selectscallsreceives instance ascallerobjectmethodselfcurrent object
How does writing object.method() cause the method to run with that particular object supplied as self?

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?

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

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

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

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

Mistakes with Instance Members

  • Treating self as an ordinary unrelated parameter.

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

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

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

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

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

  • Ignoring the object before the dot.

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

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

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. An object is a constructed instance of a class and contains attributes and methods defined by that class.
  2. Instance attributes store data associated with an object instance.
  3. Instance methods provide behavior that can work with the object's data.
  4. The first parameter of an instance method is conventionally named self and gives access to the current object instance.
  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.

Key Takeaways

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