Answers for "using super python"

15

python super

# It's kinda hard to explain this just by code.
# So I'll provide a link to a pretty good explanation of it.
https://www.pythonforbeginners.com/super/working-python-super-function
Posted by: Guest on July-28-2020
5

how to get the parent class using super python

class Foo(Bar):

    def __init__(self, *args, **kwargs):
        # invoke Bar.__init__
        super().__init__(*args, **kwargs)
Posted by: Guest on August-13-2020
2

python super example

# what's super() function for, might help you a little

class parent:
    def __init__(self):
        self.A = 'A'
        self.B = 'B'
    def get(self):
        return self.A

class child1(parent):
    def __init__(self):
        super().__init__()
        self.C = 'C'
    # def get(self):
        # return self.A

class child2(parent):
    def __init__(self):
        super().__init__()
        self.C = 'C'
    def get(self):
        # return self.A
        return self.C

class child3(parent):
    def __init__(self):
        super().__init__()
        self.C = 'C'
    def get(self):
        # return self.A
        T = super().get()
        return T + self.C

class child4(parent):
    def __init__(self):
        # super().__init__()
        self.C = 'C'
    def get(self):
        # return self.A
        return self.C

class child5(parent):
    def __init__(self):
        # super().__init__()
        self.C = 'C'
    # def get(self):
        # return self.A

pa = parent()
c1 = child1()
c2 = child2()
c3 = child3()
c4 = child4()
c5 = child5()

print(pa.get()) # 'A'

# we use get func from taken from parent class
print(c1.get()) # 'A'

# get function has been overrides
print(c2.get()) # 'C'

# calling get function from parent then do whatever we want
print(c3.get()) # 'AC'

# get function has been overrides, same like child4
print(c4.get()) # 'C'

# throw an exception, get function from parent returning var A
# but child5 don't have it
try:
    # AttributeError: 'child5' object has no attribute 'A'
    print(c5.get())
except AttributeError as err:
    print('AttributeError:', err)

# check this:
print(parent.get) # function parent.get
print(child1.get) # function parent.get
print(child2.get) # function child2.get
print(child3.get) # function child3.get
print(child4.get) # function child4.get
print(child5.get) # function parent.get

###############################################################################
# a little example

class point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    # you might want know about this things:
    # __add__, __sub__, __mul__, __truediv__, ...
    def __add__(self, value):
        return self.x + value, self.y + value

class point3D(point):
    def __init__(self, x, y, z):
        super().__init__(x, y)
        self.z = z

    def __add__(self, value):
        return *super().__add__(value), self.z + value
        # equivalent to:
        # t = super().__add__(value)
        # return t[0], t[1], self.z + value

A = point(10, 20)
B = point3D(100, 200, 300)

print(A) # __main__.point object
print(B) # __main__.point3D object 

print(A + 2) # (12, 22)
print(B + 2) # (102, 202, 302)

print(type(A)) # class '__main__.point'
print(type(B)) # class '__main__.point3D'

print(type(A) == point) # True
print(type(B) == point) # False

print(type(A) == point3D) # False
print(type(B) == point3D) # True

print(isinstance(A, point)) # True
print(isinstance(B, point)) # True

print(isinstance(A, point3D)) # False
print(isinstance(B, point3D)) # True
Posted by: Guest on August-07-2021
12

python super

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)
Posted by: Guest on March-08-2020

Python Answers by Framework

Browse Popular Code Answers by Language