Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python and using 'self' in methods

Tags:

python

self

From what I read/understand, the 'self' parameter is similiar to 'this'.

Is that true?

If its optional, what would you do if self wasnt' passed into the method?

like image 511
Blankman Avatar asked Jul 19 '10 16:07

Blankman


People also ask

What is the use of self in Python method?

The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

What is self In __ init __ method?

The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.

Is self a parameter in Python?

In Python, the word self is the first parameter of methods that represents the instance of the class. Therefore, in order to call attributes and methods of a class, the programmer needs to use self .

Can I use self in static method Python?

A static method is precisely one which does not take self because it does not need to call other instance methods, or would do so via the class name.


1 Answers

Yes, it's used in similar ways. Note that it's a positional parameter and you can call it what you want; however there is a strong convention to call it self (not this or anything else). Some positional parameter must be there for a usable instance method; it is not optional.

like image 84
Matthew Flaschen Avatar answered Oct 16 '22 01:10

Matthew Flaschen