This question already has answers here:
What does object's __init__() method do in python? [duplicate]
(3 answers)
Closed 9 years ago.
class subclass(superclass):
def __init__(self, arg1, arg2):
superclass.__init__(self, blah1, blah2)
What is the purpose of using superclass.__init__(self, blah1, blah2)?
I am a little confused regarding whether to use the last line or not while inheriting a superclass.
superclass.__init__(self,*args,**kwargs)
is essentially equivelent to
super(Myclass,self).__init__(*args,**kwargs)
that is it calls the supers constructor. but it skips the rest of the inheritance stack (I think super() bubbles or something... most of the time i use the first method)
**this is probably an over simplification
Related
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:
Does the #staticmethod decorator do anything?
(2 answers)
Closed 3 years ago.
Given that the following code runs without error, why is the staticmethod decorator necessary? What is it actually doing?
class Foo:
def say_hi(name):
print(f'Hello {name}.')
#staticmethod
def say_bye(name):
print(f'See ya later, {name}.')
my_name = 'Bar...t'
Foo.say_hi(my_name)
Foo.say_bye(my_name)
Static methods, much like class methods, are methods that are bound to a class rather than its object.
They do not require a class instance creation. So, they are not dependent on the state of the object.
The difference between a static method and a class method is:
Static method knows nothing about the class and just deals with the parameters.
Class method works with the class since its parameter is always the class itself.
https://www.programiz.com/python-programming/methods/built-in/staticmethod
This question already has answers here:
Making functions non override-able
(6 answers)
Prevent function overriding in Python [duplicate]
(3 answers)
How to prevent a function from being overridden in Python [duplicate]
(4 answers)
Closed 3 years ago.
With python abstract classes, is there a way to require that a class instance does not override a particular method?
Using the #abstractmethod decorator on a method requires the child class to define the method. Skipping the #abstractmethod decorator allows the child to skip defining the method and "silently inherent" the abstract method. What I'd like is something like a #onlyabstractmethod to decorate a method that I want to ensure the child does not override.
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?