<< Chapter < Page Chapter >> Page >

Methods are often defined with a special argument as the first argument in the argument list. This argument is named self in the methods of Listing 3 . Here is some of what The Python Tutorial -- Random Remarks has to say about self .

Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.

Therefore, even though it is possible to give the first argument a different name, I will stick with the name self in this module.

The first argument in a function's argument list is special because:

  • When an object's method is called using the syntax shown in Listing 2 , the first incoming parameter to the method is an identification of the chunk of memory belonging to the objectthat was used to make the call.
  • The client code that makes the call is not responsible for including that information in the argument list when the call is made. The interpretertakes care of that task automatically.

For example, the playStation method shown in Listing 3 shows two required positional arguments including the argument named self . The call to the playStation method shown in Listing 2 passes only one parameter. The argument identified as self in Listing 3 is automatically constructed and passed to the method by the Python interpreter.

When you write code in the body of the method, you can use the value of self along with the dot operator to access variables and methods belonging to the object using code such as that shown in Listing 4 .

Listing 4 . Using self to access an instance variable.
print("Playing " + self.stationNumber[index-1])

As near as I have been able to determine, references to an object's instance variables or methods by code within the body of a method that is defined in thatobject's class must qualify those references with self (or whatever name you choose to give to the method's first argument) using the dot operator.

That restriction does not apply to class variables. I will have more to say about that in a future module. There are probably other exceptions as wellhaving to do with communications between methods in different objects or methods in one object accessing variables in a different object.

Class methods versus instance methods

In other OOP environments, we would probably refer to the method named playStation in Listing 3 as an instance method . The method is called using an object's reference as shown in Listing 2 . That object's reference is passed into the method's self parameter as discussed above.

It is also possible to call a method in a class using the name of the class in place of an object's reference. In that case, an object's reference is notautomatically passed to a self parameter. In fact the method does not even need to define a self parameter. In other programming environments, we would probably refer to such a method as a class method . Class methods are typically used to perform an action that is independent of anyspecific object, such as computing the square root of a number for example.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask