Concepts / Attributes and Instance Variables

Attributes and Instance Variables

A method is a function that belongs to a class and includes a self parameter that refers to the specific object the method is called on.

  • Programming

Behavior Belongs with Data

A method is a function that belongs to a class. Its purpose is to perform behavior for an object, while self identifies the particular object receiving that behavior. This connection lets a method work with the object's attributes and helps bundle behavior with data in an organized, maintainable way.

When you see a method call such as p.say_hi(), read it as: ask the object p to perform its say_hi behavior.

Defining a Method Inside a Class

python

The def keyword defines the function. Because the def block is placed inside Person, say_hi is a method of that class. The first parameter is named self. In this example, self is the only parameter, so the method needs no additional information from the caller.

containsreceivesPersonsay_himethodselfobject receiving the call
How does placing a def block inside a class make the function belong to that class?
introducesdeclares parameterdefdefines a functionsay_himethod nameselfcalling object
What does self represent in a class method, and how is it different from other parameters?

How Python Supplies self

Tracing p.say_hi()

Determine which object self refers to when the method is called as p.say_hi().

Create an instance: p refers to an instance of Person.

Call the method: The dot notation p.say_hi() invokes say_hi on that instance.

Bind self: Python automatically passes p as the self argument to say_hi.

Run the method body: Inside the method, self refers to p, so the print statement runs on behalf of that object.

self refers to p during this method call, and the method prints Hi!

python
Output
Hi!
calls say_hi()receives p asruns method bodypPerson instancesay_himethodselfpHi!printed output
When a method is called on an object, how does self become linked to that particular instance?

Connecting self to Attributes

An object's attributes are data associated with that object. self gives a method access to the attributes of the particular object that called it. Therefore, the same method definition can operate on different instances: self identifies which object's data the method should use.

python
Output
Asha

In this generated example, self.name refers to the name attribute on p because p is the object that called introduce. The important link is not the particular name value; it is the path from the method to the calling object's attributes through self.

identifieshas attributeusespPerson instancenameAshaselfrefers to pintroducemethod
How does self let a method access the attributes stored inside the object that called it?

Calling Without Extra Arguments

A method whose definition contains only self is called without an argument written inside the parentheses. Python supplies the instance automatically. The caller writes p.say_hi(), not p.say_hi(p), because the object named p is already the object on which the method is invoked.

python
Output
Hi!
Hi!

The two calls produce the same output. In the first call, p is stored and then used to invoke say_hi. In the second, Person() creates a temporary Person object and the method is called on it immediately. Because that temporary object is not stored in a variable for later use, it is discarded after the call.

invokesbindsexecutespinstancesay_hi()no written argumentsselfp supplied automaticallyHi!output
What is passed implicitly when an instance method is called with no arguments beyond self?

Mistakes with self

  • Leaving self out of the method definition

    The method has no parameter that can receive the object automatically passed during an instance call.

    Fix: Write def say_hi(self): even when the method needs no other parameters.

  • Passing the instance manually in ordinary dot notation

    The object p is already supplied as self when the method is invoked through p.

    Fix: Use p.say_hi() when say_hi takes only self.

  • Treating self as a separate object created by the method

    self refers to the specific object on which the method was called.

    Fix: Trace the receiver of the dot call. In p.say_hi(), self refers to p.

  • Assuming a method with only self cannot work with object data

    self gives the method access to the calling object's attributes.

    Fix: Use self to refer to attributes associated with the instance.

Practice the Binding

EASY

Consider this class definition: class Device: def identify(self): print(self.label) Which object does self refer to when the call is made through an instance named phone? Explain why the method call does not need an argument inside its parentheses.

Hints
  • Look at the object immediately to the left of the dot.
  • The method definition includes self as its only parameter.
  • Python automatically passes the calling object as self.

Key Takeaways

  1. A method is a function that belongs to a class.
  2. Every method definition requires self, even when there are no additional parameters.
  3. When an instance invokes a method with dot notation, Python automatically passes that instance as self.
  4. Inside the method, self identifies the object's attributes and lets the method act on behalf of that object.
  5. Methods bundle behavior with data, supporting more organized and maintainable object-oriented programs.

Key Takeaways

  • A method is a class-owned function that uses self to identify its calling object.
  • self must appear in every method definition, even when the method takes no other parameters.
  • In p.say_hi(), Python automatically binds p to self.
  • Through self, a method can access the attributes of the instance that called it.
  • Methods combine object data and behavior into an organized unit.