This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 5 years ago.
How to use "self" keyword regarding variables? It seems that you can set a class variable inside of __init__ constructor by using "self" prefix???
self is just a name used as a convention to refer to the instance on which methods are bound. Bound methods are always called with the instance as first argument, and you can name that variable anything.
By using self in an instance method, we set instance variables and not class ones. Different programming languages provide mechanisms to access the instance some use implicit this objects, some implicitly call all methods on the instance, and Python explicitly uses passes the instance as the first variable.
Related
This question already has answers here:
When should I be using classes in Python?
(6 answers)
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 8 months ago.
What's the point of creating classes and self in python? Like:
class ThisIsClass(self):
def Func():
self.var+=1
ThisIsClass()
Is it necessary to use them?
It becomes necessary to pass self argument to the functions of your class. however, your code has several problems like the initialization of var variable. You need a constructor __init__ to create the variable, after that, you will be able to modify it:
class ThisIsClass:
def __init__(self, value=0):
self.var = value
def Func(self):
self.var+=1
c = ThisIsClass()
c.Func()
print(c.var)
In the above code, the class is instantiated as an object and stored in the variable c. You can assign an initial value to var by passing an argument to the object creation, like: c = ThisIsClass(89)
Output:
1
It has to do with the right structure of programming in python. No you dont need them 100% but if you want to have a nice and clean code I would propose you to get used of them. Also when the self is declared inside the brackets after the function name, that function can retrieve (and make available) the variable (self.variable) from and to wherever inside that class.
This question already has answers here:
What's the difference between a method and a function?
(41 answers)
Closed last year.
Below is a simple class, which is having one method/function in it
class Test():
def f(self):
return "function or method"
What should I call f() here - a method or a function?
From python official document's glossary:
method
A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self). See function and nested scope.
I'd say that is a method, since it is associated with the object of the class that it belongs to. I extracted the information from here.
This question already has answers here:
Python super() arguments: why not super(obj)?
(2 answers)
How is super() in Python 3 implemented?
(2 answers)
When do you need to pass arguments to python super()?
(2 answers)
Closed 2 years ago.
Why don't we need self reference when we use super().__init__?(such as line 9 down below)
class labourers():
def __init__(self,name,department,salary):
self.name = name
self.department = department
self.salary = salary
class managers(labourers):
def __init__(self,name,department,salary,numberofpeople):
super().__init__(name,department,salary)
self.numberofpeople = numberofpeople
Super's functionality in this case is implemented in the CPython parser. See PEP 3135
Replacing the old usage of super, calls to the next class in the MRO (method resolution order) can be made without explicitly passing the class object (although doing so will still be supported). Every function will have a cell named __class__ that contains the class object that the function is defined in.
The new syntax:
super()
is equivalent to:
super(__class__, <firstarg>)
[...]
While super is not a reserved word, the parser recognizes the use of super in a method definition and only passes in the __class__ cell when this is found. Thus, calling a global alias of super without arguments will not necessarily work.
Emphasis added.
This question already has answers here:
When is "self" required?
(3 answers)
Closed 8 years ago.
In methods when is it necessary to use notation like self.variable_name? For instance, I know that in the constructor method it needs to be like
class A(object):
def __init__(self, name):
self.name = name
in order to give it an instance variable. However, what about in other methods? When do I need to put self in front of a variable name and when is it okay to just use a variable name?
You must always put a self as the first argument of an instance method. This is what you use to access the instance variables.
It is similar to this in other languages, but different in that it is required whereas other languages will often have instance variables in scope.
Whenever you are wanting to access attributes of the particular object of type A. For example:
def get_name(self): # here, whenever this method is called, it expects 'self'.
return self.name
When calling this method outside of the class scope, python has self passed implicitly.
example = A('d_rez')
example.get_name() # no arguments are passed, but self is passed implicitly to
# have access to all its attributes!
So to answer your question: You need to pass self whenever you define a method inside a class, and this is usually because there are some attributes involved. Otherwise, it could just be a static function defined outside the class if it doesn't pertain to a particular instance of class A.
This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 9 years ago.
I am learning OOP, so my question on the two statements below probably seems very basic to many, but I just want to check in case I am using Python/OO lingo in the wrong way:
- You must explicitly define 'self' as the first parameter in every class method.
- Python's explicit requirement of self as the first parameter of methods defined in classes, makes it so that it's clear to people what the difference between instance and static methods are.
If the statements are correct concerning the 'self' keyword, is it correct of me to infer that, either as a parameter in method definitions or as a prefix to variables; the presence of the word 'self' commonly indicates that a method or variable is static, whereas the absence of the word 'self' usually indicates that the method or variable is an instance method or variable?
The name of the first parameter is not syntactically important. It's self for normal methods by convention, but that's not enforced.
What is syntactically important is how the method is constructed. A method is a normal method and will receive the instance it's called on as its first argument unless converted into a class method or static method by the classmethod or staticmethod built in functions.
A class method receives the class as its first argument, which by convention is named cls. Although because class methods are less common than normal methods, I've seen more deviations from that convention than I have for normal methods. klass is not totally uncommon, and sometimes people are careless and use self out of habit.
A static method does not receive any automatic arguments.
class ExampleClass(object):
def sample_instance_method(self):
# this is a normal instance method
pass
#classmethod
def sample_class_method(cls):
# this is a class method, and will not receive the individual instance if called on one
pass
#staticmethod
def sample_static_method():
# this is a static method, and will not receive either the instance or the class when called
pass
No, its the other way round.
But I can see why the doubt, these statements are confusing: you put self as first argument in instance methods, and you don't put it in static methods.
Note that although there is no need in naming that first parameter self, it is better to follow that convention as it is clearer to the reader.
Also, don't name selfthe first parameter in a static method, it is possible, yes, but you will for sure confuse the reader.