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

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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