position of super().__init__() gives different output - python-3.x

I was trying to understand this example working.
class P:
def __init__(self):
self.__x=100
self.y=200
def print(self):
print(self.__x, self.y)
class C(P):
def __init__(self):
super().__init__() <-------------------
self.__x=300
self.y=400
d = C()
d.print()
output: 100 400
class P:
def __init__(self):
self.__x=100
self.y=200
def print(self):
print(self.__x, self.y)
class C(P):
def __init__(self):
self.__x=300
self.y=400
super().__init__() <--------------------
d = C()
d.print()
output: 100 200
Can someone explain the execution flow of the above codes leading to separate outputs?

Related

Pyside6 mouse coordinate relative to image

Let suppose I have an image correctly rendered in a QLabel
As I want to get mouse position relative to the image I'm subclassing QLabel and reimplant mouseMoveEvent(self,event) and then build the source_image from this class.Rendering is Ok, but how do I get the event.x() from the parent class ?
I know I have to connect the instance to the ImLabel signals but i'm stuck with implementation.
class ImLabel(QLabel):
def __init__(self):
super().__init__()
on_mouse_move = Signal()
def mouseMoveEvent(self, event):
print(event.x(), event.y())
super(ImLabel, self).mouseMoveEvent(event)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.source_image = ImLabel()
self.source_image.on_mouse_move.connect(self.mouseMoveEvent)
...
self.cursor = QLabel("cursor : ")
...
def mouseMoveEvent(self,event):
self.cursor.setText('Mouse coords: ( %d : %d )' % (event.x(), event.y()))
Finnally this is the way I made it :
class ImLabel(QLabel):
def __init__(self):
super().__init__()
def mouseMoveEvent(self, event):
super(ImLabel, self).mouseMoveEvent(event)
self.x,self.y = event.x(),event.y()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
...
def mouseMoveEvent(self,event):
self.cursor.setText('Mouse coords: ( %d : %d )' % (self.source_image.x,self.source_image.y))

Inheritance with python unittest

I have two unittest classes in the same file which look like this:
class A(unittest.TestCase):
def some_fun(param):
# Set class parameters
self.foo = param + 100
...
def some_fun2():
do_something with self.foo
self.assertEqual(self.foo,<check>)
def some_fun3():
do_something
def setUp(self):
some_fun(some_foo1)
def test1(self):
some_fun2(some_foo1)
def tearDown(self):
some_fun3(some_foo1)
class B(unittest.TestCase):
def some_fun(param):
# Set class parameters
self.foo = param + 100
...
def some_fun2():
do_something with self.foo
self.assertEqual(self.foo,<check>)
def some_fun3():
do_something
def setUp(self):
some_fun(some_foo1)
def test1(self):
some_fun2(some_foo1)
def tearDown(self):
some_fun3(some_foo1)
I want to move some_fun, some_fun2, some_fun3 outside to reduce code repetition keeping in mind that these functions can have assert checks
What is the best way to do this? Im not sure how to do inheritance in this scenario
Edit:
I am facing this weird issue
class X(unittest.TestCase):
def __init__(self, foo):
print(foo) # why is foo=somefun
def test():
<something>
class A(X):
FOO = "SOMESTRING"
def setUp(self):
super().__init__(A.FOO)
def somefun(self):
self.test()
It can be like this:
class X(unittest.TestCase):
def some_fun(self, param):
# Set class parameters
self.foo = param + 100
...
def some_fun2(self):
do_something with self.foo
self.assertEqual(self.foo,<check>)
def some_fun3():
do_something
class A(X):
def setUp(self):
self.some_fun(some_foo1)
def test1(self):
self.some_fun2(some_foo1)
def tearDown(self):
self.some_fun3(some_foo1)
class B(X):
def setUp(self):
self.some_fun(some_foo1)
def test1(self):
self.some_fun2(some_foo1)
def tearDown(self):
self.some_fun3(some_foo1)
Inheritance rules apply normally here. Create a base class with the methods you want that inherits from TestCase:
class BaseTest(unittest.TestCase):
def some_fun(self):
...
def some_fun2(self):
...
Create A and B inheriting from BaseTest:
class A(BaseTest):
def test1(self):
...
class B(BaseTest):
def test2(self):
...

How to Access Outer class variables in Inner Class (Python)?

How to access the variables of the outer class in the Inner Class?
class Student:
def __init__(self,Name,rollNumber):
self.Name=Name
self.rollNumber=rollNumber
self.lap=self.Laptop()
def Show(self):
print(self.Name)
print(self.lap.show())
class Laptop:
def __init__(self):
self.brand = "Mac"
self.cpu = "i9"
self.ram = 16
def show(self):
return self.brand
#staticmethod
def Show():
return s1.Name
s1=Student("Teja",2)
print(s1.Name,s1.rollNumber)
s1.Show()
print(s1.lap.brand)
system=s1.lap
print(system.brand)
print(system.cpu)
print(system.show())
print(system.Show())

Python Class Multiple Inheritance Only Inherit Variables From One Parent?

I'm confused about the way Python class inherit from multiple parent classes.
If the parent classes all inherit from the same grand-parent class, everything is wonderful.
# grand-parent class
class Z():
def __init__(self):
pass
# parent class A
class A(Z):
def __init__(self):
super().__init__()
self.x = 1
# parent class B
class B(Z):
def __init__(self):
super().__init__()
self.y = 2
# parent class C
class C(Z):
def __init__(self):
super().__init__()
self.z = 3
# target class D
class D(A, B, C):
def __init__(self):
super().__init__()
d = D()
print(vars(d))
#{'x': 1, 'y': 2, 'z': 3}
Without the same grand-parent class, only variables from the first parent class is inherited.
# parent class A
class A():
def __init__(self):
self.x = 1
# parent class B
class B():
def __init__(self):
self.y = 2
# parent class C
class C():
def __init__(self):
self.z = 3
# target class D
class D(A, B, C):
def __init__(self):
super().__init__()
d = D()
print(vars(d))
#{'x': 1}
Python's method resolution order works from Left to Right. It will only call the init method of the first class(A in your case).
This will give you the desired result-
class A():
def __init__(self):
super().__init__()
self.x = 1
# parent class B
class B():
def __init__(self):
super().__init__()
self.y = 2
# parent class C
class C():
def __init__(self):
self.z = 3
# target class D
class D(A, B, C):
def __init__(self):
super().__init__()
d = D()
print(vars(d))

Override abstract setter of property in Python 3

What is the simplest / most pythonic way to override only the setter of an abstract property in Python 3? Variant 3 seems to mean the least effort for the derived class implementor. Is it correct? Does it have disadvantages?
import abc
class A1(metaclass=abc.ABCMeta):
def __init__(self, x, **kwargs):
super().__init__(**kwargs)
self._x = x
#property
def x(self):
return self._x
#x.setter
#abc.abstractmethod
def x(self, value):
self._x = value
class B1(A1):
#property
def x(self):
return super().x
#x.setter
def x(self, value):
print("B1 setter")
super(B1, self.__class__).x.fset(self, value)
b1 = B1(x=1)
b1.x = 3
print(b1.x)
class A2(metaclass=abc.ABCMeta):
def __init__(self, x, **kwargs):
super().__init__(**kwargs)
self._x = x
#abc.abstractmethod
def _get_x(self):
return self._x
#abc.abstractmethod
def _set_x(self, value):
self._x = value
x = property(_get_x, _set_x)
class B2(A2):
def _get_x(self):
return super()._get_x()
def _set_x(self, value):
print("B2 setter")
super()._set_x(value)
x = property(_get_x, _set_x)
b2 = B2(x=1)
b2.x = 3
print(b2.x)
class A3(metaclass=abc.ABCMeta):
def __init__(self, x, **kwargs):
super().__init__(**kwargs)
self._x = x
def _get_x(self):
return self._x
#abc.abstractmethod
def _set_x(self, value):
self._x = value
x = property(
lambda self: self._get_x(),
lambda self, value: self._set_x(value))
class B3(A3):
def _set_x(self, value):
print("B3 setter")
super()._set_x(value)
b3 = B3(x=1)
b3.x = 3
print(b3.x)
So, yes, you listed a lot of ways in there - and although the one that requires more code is your variant 3, the most straighforard, least surprising way to do it is your variant 1 -
It just works, and is perfectly readable, no surprises - and there seems to be no simpler way than calling fget explicitly there.

Resources