Linked Questions
369 questions linked to/from What is the purpose of the `self` parameter? Why is it needed?
981 votes
18 answers
1.6m views
What do __init__ and self do in Python? [duplicate]
I'm learning the Python programming language and I've came across something I don't fully understand. In a method like: def method(self, blah): def __init__(?): .... .... What does ...
225 votes
10 answers
115k views
Why do you need explicitly have the "self" argument in a Python method? [duplicate]
When defining a method on a class in Python, it looks something like this: class MyClass(object): def __init__(self, x, y): self.x = x self.y = y But in some other languages, such ...
61 votes
4 answers
65k views
Python 'self' keyword [duplicate]
I am new to Python (usually work on C#), started using it over the last couple of days. Within a class, do you need to prefix any call to that classes data members and methods? So, if I am calling a ...
39 votes
8 answers
100k views
Explaining the 'self' variable to a beginner [duplicate]
I'm pretty much ignorant of OOP jargon and concepts. I know conceptually what an object is, and that objects have methods. I even understand that in python, classes are objects! That's cool, I just ...
78 votes
6 answers
177k views
Python calling method without 'self' [duplicate]
So I just started programming in python and I don't understand the whole reasoning behind 'self'. I understand that it is used almost like a global variable, so that data can be passed between ...
52 votes
6 answers
163k views
Method arguments in Python [duplicate]
Suppose I have this code: class Num: def __init__(self,num): self.n = num def getn(self): return self.n def getone(): return 1 myObj = Num(3) print(myObj.getn()) # ...
41 votes
4 answers
74k views
When do you use 'self' in Python? [duplicate]
Are you supposed to use self when referencing a member function in Python (within the same module)? More generally, I was wondering when it is required to use self, not just for methods but for ...
12 votes
4 answers
6k views
Why accessing to class variable from within the class needs "self." in Python? [duplicate]
I'm learning Python and I have a question, more theoretical than practical, regarding access class variables from method of this class. For example we have: class ExampleClass: x = 123 def ...
20 votes
1 answer
77k views
Function() takes exactly 2 arguments (3 given) [duplicate]
I am using python to call a method in one class that is in one file from a method in another class of other file Suppose my file is abc.py that contains class data : def values_to_insert(...
8 votes
2 answers
11k views
Can python have class or instance methods that do not have "self" as the first argument? [duplicate]
Every single example I have seen of a method in a class in Python, has self as the first argument. Is this true of all methods? If so, couldn't python have been written so that this argument was ...
6 votes
6 answers
2k views
python and using 'self' in methods [duplicate]
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?
1 vote
4 answers
6k views
Why do functions/methods in python need self as parameter? [duplicate]
I can understand why it is needed for local variables, (self.x), but why is is nessecary as parameter in a function? Is there something else you could put there instead of self? Please explain in as ...
4 votes
1 answer
8k views
what "self" is doing in the selenium python code? [duplicate]
Possible Duplicate: Python ‘self’ explained I just wrote a code as below with the help of selenium documentation, but confused with one what self does some methods argument list? Why I need to ...
0 votes
2 answers
4k views
Local Variable "age" referenced before assignment [duplicate]
I recently started learning python. And im working on a problem. class Person: age = 0 def __init__(self,initial_Age): if initial_Age<0: age=0 print("This ...
0 votes
1 answer
11k views
TypeError: method() takes exactly 2 arguments (3 given) [duplicate]
Here is a code: class Child(object): def chunks(l, n): """ Yield successive n-sized chunks from l. """ for i in xrange(0, len(l), n): yield l[i:i+n] k= range(...