What does self do in Python?
Answer
The self is used to represent the instance of the class. With this "keyword", you can access the attributes and methods of the class instance from within the class.
Self is a first parameter of constructor or class method. The name self is a common convention, however it any other name can be used provided that the parameter is the first parameter in the method.
When a method is defined as static method (by decorating it using @staticmethod decorator), it doesn't have the self parameter because static method doesn't allow to access instance of a class.
Example:
class MyClass:
__init__(self):
self.name = "MyClass Human-readable name"
self.year = 2145
def modify_year(self, new_year: int):
self.year = new_year
@staticmethod
def log(message: str):
print(message)
Related Python job interview questions
How do you split long method signature to multiple lines in the most efficient way?
Python JuniorHow do you split long line to multiple lines in Python?
Python JuniorHow do you install a package using PIP?
Python JuniorHow do you check the currently installed version of PIP?
Python JuniorHow do you install list of packages installed by PIP?
Python Junior
Chat
Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.
e
@@xeDO0
1'"
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
VTgcz37T'; waitfor delay '0:0:15' --
1 waitfor delay '0:0:15' --
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
if(now()=sysdate(),sleep(15),0)
-1" OR 3+906-906-1=0+0+0+1 --
-1" OR 2+906-906-1=0+0+0+1 --
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 3+137-137-1=0+0+0+1 --
-1' OR 2+137-137-1=0+0+0+1 --
-1 OR 3+877-877-1=0+0+0+1
-1 OR 2+877-877-1=0+0+0+1
-1 OR 3+418-418-1=0+0+0+1 --
-1 OR 2+418-418-1=0+0+0+1 --
e
e