28 Respostas
+ 18
You need to add "self" as the first argument of every class method because "self" is a reference to the current instance of the class, and you'll know why explicit "self" has to stay in python by Guido van rossum
http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html
+ 17
From my understanding of OOP in Python, self is basically just a placeholder for the name of the class when you're referring to it outside of the class itself.
For example:
class Test:
def __init__(self, x, y):
self.hi = x
self.hello = y
z = Test(5, 6)
print(z.hi)
print(z.hello) #In the class, self just represents the name of the variable that created the class, in this case being z
+ 10
It is similar to "this" pointer in c++. If you are familiar with c++, then you can relate them.
+ 8
self is a scope variable which refers to the current instance of the class allowing you to access it's properties and methods.
+ 7
if you ask me , I think it just a syntax thing because other languages don't require you to use it to indicate that ur referencing the current object but in some cases u do need it. take for example JavaScript, in JavaScript u don't use the word "this" when declaring methods but you do have to use it when declaring a variable. so really it's a matter of design. The less you have to write or worry about the higher level the language is.
the more you write the lower than ge is.
+ 7
'Self' is an identifier that the function uses to recognize an object within the class's attributes. It's a way to access/call the attributes of the object created within the class.
For example:
class Company:
def __init__(self): #Constructor using 'self' to initialize starting values of the object being created
self._nameOfCompany = ""
self._salesAmt = 0.0
def setName(self,argument): #'self' in this function is used to call and change the attribute 'nameOfCompany'
self._nameOfCompany = argument
def weirdFunction(): #if for some reason, there's a function within the class that doesn't need access to the attributes of the object, you don't need to put in 'self'.
2 == 2
+ 6
used mostly for accessing constructor varaiables
+ 6
self is a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
+ 6
A represenation of the object you will create of that class
+ 5
and just to complete what the others said, u can use any other word insteed of self but self is agreed to be used form the world developers communiy
+ 5
ive understood it from the comments
+ 5
The "self" parameter is an object, and its purpose is to call the method of the class.
For example, in the code below:
class Cat:
def __init__(self, color):
self.color = color
the class is "Cat" and the method is "color". So, for any animal cat , you have the corresponding color.
It's mandatory !!!
+ 4
thanks everyone here for helping me out
+ 4
It’s just the conventional way of doing it. At least that’s what I was taught.
+ 4
Thanks Paavan Gupta, Ur explanation is really helpful 👍👌❤️
+ 3
From my understanding of OOP in Python, self is basically just a placeholder for the name of the class when you're referring to it outside of the class itself.
For example:
class Test:
def __init__(self, x, y):
self.hi = x
self.hello = y
z = Test(5, 6)
print(z.hi)
print(z.hello) #In the class, self just represents the name of the variable that created the class, in this case being z
+ 3
For clarification:
https://code.sololearn.com/cjgeT97o58IT/?ref=app
+ 2
self is used to refer an object of a class even before the object is created.
if you know c++ or javascript then you may have learned about the 'this' pointer.
the same work is of the self keyword in python.
for an example, if you want to make a class of employees then you may need to increment the salary of an employee certain times. now, if the employee object is not yet created then how can you increase the salary of that particular employee. here you use the self keyword.
class employee:
def __init__(self, salary):
self.salary = salary
def increamentSalary (self, increase):
self.salary = self.salary + increase
+ 2
what feature is unique in python that cannot be found in other computer languages?
+ 2
@Moses_Nyamai (Features of Python):
1. Python is easy to learn because of easy syntex. So you don't have to memorise hard syntex that other languages have. Instead you can learn the actual programming
2. Python has a huge library for data analysing, web crawling, back-nd programming etc. So it's fun to play with them
3. Many good websites like Google, Dropbox, Instagram, Twitter uses python hugely
4. And of course for Beginner's learning Python is no doubt the best