Super() in classes

hi
I saw in some classes that there is a super() statement as super().init or super().c() or super() without any tail.
what is it and for what is used?plz, explain
thanks

Putting python super into a search engine immediately gives many thorough explanations; did you have a more specific question?

2 Likes

hi
I think when anyone poses a problem or question, first he has searched the internet and he has not taken satisfactory or complete results, so pose here.
thanks for your reply

My experience has taught me that this is very often not true, but of course I trust you.

Still, it won’t be possible for me to explain any better than the results from a search engine - because I don’t know which ones you saw, and I don’t know what you were still confused by after reading them, or what you didn’t find satisfactory or complete about them.

2 Likes

Maybe you could post a few specific examples you’ve seen, perhaps we can
comment.

what does super() in below code?

class Immutable:
    def __init__(self, value):
        super().__setattr__("value", value)

    def __setattr__(self, name, attr_value):
        raise AttributeError(f"can't set attribute '{name}'")

    def __delattr__(self, name):
        raise AttributeError(f"can't delete attribute '{name}'")

or what do super()'s in the below code?

# super-%D8%AF%D8%B1-%D8%A7%D8%B1%D8%AB-%D8%A8%D8%B1%DB%8C-%D8%A7%D8%B2-
# %DA%86%D9%86%D8%AF-%DA%A9%D9%84%D8%A7%D8%B3-%D8%AF%D8%B1-
# %D9%BE%D8%A7%DB%8C%D8%AA%D9%88%D9%86

# usage of super()

class First():
    def __init__(self):
        print ("first")
 
class Second(First):
    def __init__(self):
        super().__init__()
        print ("second")
 
class Third(First):
    def __init__(self):
        super()
        print ("third")
 
class Fourth(Second ,Third):
    def __init__(self):
        super().__init__()
        print ("that's it")
 
Fourth()
# output is:
"""
third
second
that's it
"""

class Third2(First):
    def __init__(self):
        super().__init__()
        print ("third")


class Fourth2(Second ,Third2):
    def __init__(self):
        super().__init__()
        print ("that's it")

can you explain what different supper() do in the above code?

class First():
    def __init__(self):
        print ("first")
 
class Second(First):
    def __init__(self):
        super().__init__()
        print ("second")

Second()

Can you figure out what happens in the above case?

What happens if you remove super().__init__()?

for the two above questions, my answer is no.
I do not know for what super() is used.
I can not refer here all the time(only once in two days). if know or can explain please do it.

After reading, for example, the official documentation, or this guide (which for me is the first result from the search query I linked you), what question remains? I think that these sources completely explain what super is used for, and I still don’t understand why there is confusion.

Also, did you try running the code from @gkb’s post?

1 Like

hi Karl Knechtel
I read the official document 2 or 3 times and also ran the @gkb code and saw the results of it with and without the line super()…
but I did not read the guide until now.
I think(almost surely)that super is used for the method of the superclass(parent class) and THAT a method with the same name is in the child class. is it true?
suppose A is a child of B, B is a child of C, C is a child of D and all have the init method,then I want to call init of D in class A.
how can I write the super() command?
thanks for reply

If all you want to do is have your subclass call the init method of your superclass, you don’t have to use super at all!

class First:
    def __init__(self):
        print ("first")
 
class Second(First):
    pass

Second()

Only if you overwrite the init method in Second, but still want to execute the init method of Second’s superclass, you need to use super.

1 Like

thanks for reply
suppose there is overwriting. how can call init method of class D in class A? namely, must I write in class A as below:
super(d).init()

In that case I would simply explicitly call D’s init method within A:

class A(D):
    def __init__(self):
        D.__init__(self)
        # do sth else here
1 Like