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.
Related
This question already has answers here:
What are metaclasses in Python?
(25 answers)
Closed 3 years ago.
When browsing the source code for the django_filters library, I found a class declaration syntax that I've never seen before: inheritance with arguments. I could not find an explanation in the official python class tutorial.
Here is the source, snippet below:
class FilterSet(BaseFilterSet, metaclass=FilterSetMetaclass):
pass
what does metaclass=FilterSetMetaclass in the class definition do?
There are two uses for keyword arguments in the list of base classes.
The metaclass argument is used specially to indicate which metaclass (instead of type) to use to create the class. (In Python 2, this was done by assigning a value to the class attribute __metaclass__ in the body of the class statement.)
A class statement is essentially a call to the metaclass.
class Foo(metaclass=Bar):
pass
is equivalent to Foo = Bar('Foo', (), {}). The default metaclass is type, that is
class Foo():
pass
is equivalent to
class Foo(metaclass=type):
pass
Other keyword arguments are passed along to __init_subclass__.
Both of the above are simplifications; see Customizing Class creation for more details.
This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 3 years ago.
class First:
#classmethod
def hello(self):
print(123)
class Second:
#classmethod
def hello(cls):
print(123)
obj1 = First()
obj2 = Second()
print(obj1.hello())
print(obj1.hello())
I am not getting any error while calling obj1 (with self as argument) and obj2 (with cls as argument). Why not? Is the classmethod decorator able to use cls/self?
One will be understood by every reasonably experienced Python programmer.
The other one will confuse every reasonably experienced Python programmer.
The first argument of a classmethod is a class and should be called cls. You can give it any other name in your code, but that's a bad idea, because it still is a class.
This question already has answers here:
How does the #property decorator work in Python?
(15 answers)
How do Python properties work?
(4 answers)
Closed 4 years ago.
I can do
class Foo(object):
x = property(lambda _: 123)
f = Foo()
f.x
to get 123
However, if I try
p = property(lambda : 123)
p
I get
<property object at 0x108f2f3b8>
Now I understand that an member of a class instance is not the same thing as a regular variable but I'm not sure what exactly makes this behavior different.
Does the fact that you instantiate a class somehow do extra binding on property objects? Is it a special case or is it a behavior I can take advantage in other situations and extend? Related - are property objects useful outside of a class declaration? Or is it just for this specific case?
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.
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.