Concepts / Methods and the self Parameter

Methods and the self Parameter

Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method, Python will provide it. This particular variable refers to the object itself , and by convention, it is given the name self .

  • Programming

The Missing Argument

A method can appear to take no arguments when you call it, yet its definition still contains a parameter named self. This is not a contradiction. The object that calls the method supplies that first value automatically, so the caller does not write a separate value for self.

What do you think happens?

A say_hi method has self in its definition but no value is written for self when the method is called. What supplies the self value?

  • Python supplies the object that called the method
  • The method receives no value at all
  • The programmer must always type self separately
  • The class supplies an unrelated object
Reveal answer

Answer: Python supplies the object that called the method

The first method parameter is provided automatically when the method is called from an object. By convention, that parameter is named self.

Automatic Binding

A class method has one specific difference from an ordinary function: its parameter list begins with an extra first name. By convention, that name is self. When the method is called through an object, Python provides that object as the value for self. The caller supplies values only for any parameters that follow self.

callsreceives object asPartyAnimalinstanceparty methodselfcalling object
How does an object call a method while Python supplies that object as the method's self value?

The sequence is therefore: an object calls a method, Python provides that object as the first method value, and the method uses the name self to refer to it. The caller does not provide self as a separate argument.

Mapping Values to Parameters

Tracing a party Method Call

Determine what self refers to when the party method is called from a particular PartyAnimal object.

Identify the caller: The particular PartyAnimal object before the dot is the object that calls the party method.

Identify the first parameter: The first parameter in the method definition is named self by convention.

Perform the automatic binding: Python provides the calling PartyAnimal object as the value of self. The method can therefore refer to that particular object.

Use the object through self: Inside the method, self provides access to the object instance, including its attributes and methods through dot notation.

self refers to the particular PartyAnimal instance from which party was called.

automatic mappingavailable insidePartyAnimalinstanceobject before the dotselffirst method parameterpartymethod being called
How does the calling object correspond to the first method parameter?

The key mapping is not from a separately typed argument to self. It is from the object used to make the method call to the first parameter in the method definition. That is why self can identify the correct object instance even when the method is called without an explicit self value.

What self Enables

self refers to the object itself. Because it refers to the particular object instance that called the method, it gives the method access to that object's data and behavior. The source describes this as access to attributes and the ability to call methods using dot notation.

is referred to byaccessescalls with dot notationPartyAnimalinstanceselfreference to instanceattributesobject datamethodsobject behavior
What object does self refer to inside a method, and what does that connection provide?

Ordinary Functions and Methods

FeatureOrdinary functionClass method
Parameter listDoes not require the special first parameter described hereBegins with an extra first parameter
First parameter conventionNo self convention for this distinctionThe first parameter is conventionally named self
Calling processThe caller supplies the function's argument valuesThe caller does not supply a separate value for self
Object accessThe source's method distinction does not assign the function a calling object through selfself refers to the object instance that called the method

The practical distinction is about both definition and calling. A method includes the extra first name in its parameter list, while the object used to call the method supplies the corresponding value automatically. This is why a method can use self internally without the caller writing it as an argument.

Mistakes with self

  • Assuming that a method with no caller-supplied arguments has no parameters.

    The first method parameter is present even though Python supplies its value automatically.

    Fix: Separate the method definition from the method call: self appears in the definition, while the calling object supplies its value during the call.

  • Trying to provide self as a separate argument when calling a method.

    Python provides the calling object as the value of the first parameter.

    Fix: Call the method through the object and allow Python to bind that object to self.

  • Treating self as a separate unrelated object.

    The source states that self points to the particular object instance from which the method was called.

    Fix: Interpret self as the object itself, then use it to access that object's attributes and methods.

EASY

A method named party is called from one particular PartyAnimal object. Explain what self refers to during that call and why the caller does not write a separate value for self.

Hints
  • Look at the first parameter in the method definition.
  • Identify which object made the call.
  • Remember that Python supplies the first method value automatically.

Essential Takeaways

  1. A class method has an extra first parameter in its definition.
  2. That first parameter is conventionally named self.
  3. When an object calls the method, Python automatically provides that object as self.
  4. self refers to the particular object instance that made the call.
  5. Through self, the method can access the object's attributes and call its methods using dot notation.

Key Takeaways

  • Methods differ from ordinary functions by having an extra first parameter.
  • The first parameter is conventionally named self.
  • Python supplies the calling object as self, so the caller does not provide self separately.
  • Inside the method, self refers to the particular object instance and provides access to its data and behavior.