Super and import in python












-1















What are the differences between importing a class from a file and using super() in python?



Import: # I understand that this will import all the functions and init from a class to another class in a file when trigger the below code;



from something import Some


super(): # I understand that this also will inherit all the functions and init from a class to another class in a file. **Ain't this technically the same as importing?



super(xx, self).__init__() or super().__init__() or super()


Can any python experts shows some example? I have recently read some codes online and they used both of this, my question is since both do almost the same thing(to my understanding) then why dont just use either one for all files and why need to use both? If possible, can share some examples?










share|improve this question


















  • 1





    they are totally different ... as in not at all the same thing or even similar ... I don't understand how you think they are the same thing... super is used inside of a classes methods to call methods on its parent classes ... import is to import a module

    – Joran Beasley
    Nov 20 '18 at 1:04













  • Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

    – ohmygodiamnewbie
    Nov 20 '18 at 1:31











  • A module is not a class, and functions in a module are not methods! You cannot use super to do the same as import, because the former works on classes and the later on modules.

    – MisterMiyagi
    Nov 20 '18 at 6:08
















-1















What are the differences between importing a class from a file and using super() in python?



Import: # I understand that this will import all the functions and init from a class to another class in a file when trigger the below code;



from something import Some


super(): # I understand that this also will inherit all the functions and init from a class to another class in a file. **Ain't this technically the same as importing?



super(xx, self).__init__() or super().__init__() or super()


Can any python experts shows some example? I have recently read some codes online and they used both of this, my question is since both do almost the same thing(to my understanding) then why dont just use either one for all files and why need to use both? If possible, can share some examples?










share|improve this question


















  • 1





    they are totally different ... as in not at all the same thing or even similar ... I don't understand how you think they are the same thing... super is used inside of a classes methods to call methods on its parent classes ... import is to import a module

    – Joran Beasley
    Nov 20 '18 at 1:04













  • Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

    – ohmygodiamnewbie
    Nov 20 '18 at 1:31











  • A module is not a class, and functions in a module are not methods! You cannot use super to do the same as import, because the former works on classes and the later on modules.

    – MisterMiyagi
    Nov 20 '18 at 6:08














-1












-1








-1








What are the differences between importing a class from a file and using super() in python?



Import: # I understand that this will import all the functions and init from a class to another class in a file when trigger the below code;



from something import Some


super(): # I understand that this also will inherit all the functions and init from a class to another class in a file. **Ain't this technically the same as importing?



super(xx, self).__init__() or super().__init__() or super()


Can any python experts shows some example? I have recently read some codes online and they used both of this, my question is since both do almost the same thing(to my understanding) then why dont just use either one for all files and why need to use both? If possible, can share some examples?










share|improve this question














What are the differences between importing a class from a file and using super() in python?



Import: # I understand that this will import all the functions and init from a class to another class in a file when trigger the below code;



from something import Some


super(): # I understand that this also will inherit all the functions and init from a class to another class in a file. **Ain't this technically the same as importing?



super(xx, self).__init__() or super().__init__() or super()


Can any python experts shows some example? I have recently read some codes online and they used both of this, my question is since both do almost the same thing(to my understanding) then why dont just use either one for all files and why need to use both? If possible, can share some examples?







python import super






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 0:55









ohmygodiamnewbieohmygodiamnewbie

15




15








  • 1





    they are totally different ... as in not at all the same thing or even similar ... I don't understand how you think they are the same thing... super is used inside of a classes methods to call methods on its parent classes ... import is to import a module

    – Joran Beasley
    Nov 20 '18 at 1:04













  • Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

    – ohmygodiamnewbie
    Nov 20 '18 at 1:31











  • A module is not a class, and functions in a module are not methods! You cannot use super to do the same as import, because the former works on classes and the later on modules.

    – MisterMiyagi
    Nov 20 '18 at 6:08














  • 1





    they are totally different ... as in not at all the same thing or even similar ... I don't understand how you think they are the same thing... super is used inside of a classes methods to call methods on its parent classes ... import is to import a module

    – Joran Beasley
    Nov 20 '18 at 1:04













  • Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

    – ohmygodiamnewbie
    Nov 20 '18 at 1:31











  • A module is not a class, and functions in a module are not methods! You cannot use super to do the same as import, because the former works on classes and the later on modules.

    – MisterMiyagi
    Nov 20 '18 at 6:08








1




1





they are totally different ... as in not at all the same thing or even similar ... I don't understand how you think they are the same thing... super is used inside of a classes methods to call methods on its parent classes ... import is to import a module

– Joran Beasley
Nov 20 '18 at 1:04







they are totally different ... as in not at all the same thing or even similar ... I don't understand how you think they are the same thing... super is used inside of a classes methods to call methods on its parent classes ... import is to import a module

– Joran Beasley
Nov 20 '18 at 1:04















Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

– ohmygodiamnewbie
Nov 20 '18 at 1:31





Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

– ohmygodiamnewbie
Nov 20 '18 at 1:31













A module is not a class, and functions in a module are not methods! You cannot use super to do the same as import, because the former works on classes and the later on modules.

– MisterMiyagi
Nov 20 '18 at 6:08





A module is not a class, and functions in a module are not methods! You cannot use super to do the same as import, because the former works on classes and the later on modules.

– MisterMiyagi
Nov 20 '18 at 6:08












3 Answers
3






active

oldest

votes


















0














You'll need to understand a bit of Object-oriented programming principles to understand super.



In general, when you are importing a module, that module becomes available for your current function/class.



By using Super you satisfy inheritance; facilitating your current class's this to have references to all attributes of its parent(s). If you are just developing some functions, you will not need super. This is strictly an object-oriented paradigm.



from xyz import abc

def some_function():
# here you can use abc's attributes by explicit invocation.
x = abc.x

# Or call functions.
abc.some_function()


When you're dealing with classes, super does the magic of facilitating inheritance.



class Parent_Class(object):

def __init__(self):
super(Parent_Class, self).__init__()

def parent_method(self):
print('This is a parent method')


class Child_Class(Parent_Class):

def __init__(self):
super(Child_Class, self).__init__()

def child_method(self):
# Because of super, self of child class is aware of all its parent's methods and properties.
self.parent_method()





share|improve this answer
























  • Thank you for the explanation but may I ask? What about if we have a class from another file and one class in another file? Aint just using import will import everything including the functions and attributes from first file? Example, from fileA import A within "file B". From there, we can use x = A.x or A.somefunction(). So what about this? Is the super does something different or extra on top of import when involving two different files with separate classes. From many examples, I seen all classes are within the same python file and not different file.

    – ohmygodiamnewbie
    Nov 20 '18 at 2:25











  • Also, isnt it just good enough to put super in Child_Class only? Why also in Parent_Class? Sorry I am still learning so I wanted to clear my misunderstanding

    – ohmygodiamnewbie
    Nov 20 '18 at 2:26






  • 1





    Import and super are two different things altogether. Please read and understand inheritance and some basics of OOPS. There are plenty of resources available.

    – Pruthvi Kumar
    Nov 20 '18 at 2:28













  • You'll need super in every class. Should you not do this, MRO will hurt you down the line.

    – Pruthvi Kumar
    Nov 20 '18 at 2:33











  • @ohmygodiamnewbie, accept answer if it has helped your understanding. Thanks.

    – Pruthvi Kumar
    Nov 20 '18 at 2:50



















0














import is used for importing module
__init__() is a class method, which is invoked when a new instance of a class is instantiated

This is 2 different thing



EDIT: I've made this edit to clarify the answer for you, since I assume you are not familiar with OOP. TL;DR: These two terms are completely different, they relate to two separate things. super() call a class's parent method, while import import a module




we can still import the module form file B into file A and in file A
we can still use the methods from the file B in file A right?




Right.




what makes it different than super() which calls the methods from file B when used in file A




First, you only encounter super() in subclass (lets say class B) of a class(lets say class A). You access A's method through B's instance via super(). Let see this example: You have 2 file, A.py and B.py, each define A and B:



In A.py, you define class A



class A(object):
def __init__(self):
print("Instantiate class A")

def printA(self):
print("AAAAAAAAAAAAAA")


In B.py, you define class B:



from A import A  # you need this import for `B.py` to know what is `A`

class B(A): # Mean B inherit A
def __init__(self):
super(B, self).__init__() # This line all its parent's method (`__init__()`)

def printB(self):
print("BBBBBBBBBBBB")

super(B, self).printA() # This line all its parent's method (`printA`)

# And you instantiate a `B` instance, then call a method of it, which call its super method `printA`
b = B()
b.printB()


Output is (note that `init():




Instantiate class A

BBBBBBBBBBBB

AAAAAAAAAAAAAA




To conclude, I suggest you to read some material about OOP. You will clearly see the differences btw these two things. Good luck. I hope this help!






share|improve this answer


























  • My same question to the above comment. Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

    – ohmygodiamnewbie
    Nov 20 '18 at 1:31











  • Aint it better to just use from fileA import fileB and inside file A we just use the self.function(which supposed to be in File B) and still running

    – ohmygodiamnewbie
    Nov 20 '18 at 1:32











  • I've made an edit for my answer, please read it and let me know if you still have problem

    – enamoria
    Nov 20 '18 at 4:45



















0














The import statement is used to import modules into modules. The super callable is used to delegate from subclass implementation to superclass implementation.



Notably, import connects between fixed equals whereas super connects in a dynamic hierarchy.





The point of import is to make a specific module or its content available in another module.



# moda.py
def foo(who):
print(who, "called '%s.foo'" % __name__)

# modb.py
import moda
moda.foo(__name__)


Note that importing uses a qualified name that uniquely identifies the target. At any time, the name used in an import can refer to only one specific entity. That means that import couples strongly - the target is fully identified and cannot change after the import.





The point of super is to access the closest superclass implementation in a subclass implementation.



class A:
def call(self, who):
print(who, "called '%s.call'" % __class__, "on self of class", self.__class__)

class Alpha(A):
def call(self, who):
super(Alpha, self).call(who)
print("... via '%s.call'" % __class__)


Note that super only work with local information - super does only identify the source, not the target. As such, the target is dynamically bound and not uniquely identifiable. That means that super couples weakly - the target is relatively defined and can only be resolved at runtime.



class Alpher(A):
def call(self, who):
print("'%s.call' sneakily intercepts the call..." % __class__)
super(Alpher, self).call(who)

class Better(Alpha, Alpher):
pass


The above injects Alpher into the relation from Alpha to A without modifying either of the two.



>>> Better().call('Saul')
'<class '__main__.Alpher'>.call' sneakily intercepts the call...
Saul called <class '__main__.A'>.call on self of class <class '__main__.Better'>
...via '<class '__main__.Alpha'>.call'





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384733%2fsuper-and-import-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You'll need to understand a bit of Object-oriented programming principles to understand super.



    In general, when you are importing a module, that module becomes available for your current function/class.



    By using Super you satisfy inheritance; facilitating your current class's this to have references to all attributes of its parent(s). If you are just developing some functions, you will not need super. This is strictly an object-oriented paradigm.



    from xyz import abc

    def some_function():
    # here you can use abc's attributes by explicit invocation.
    x = abc.x

    # Or call functions.
    abc.some_function()


    When you're dealing with classes, super does the magic of facilitating inheritance.



    class Parent_Class(object):

    def __init__(self):
    super(Parent_Class, self).__init__()

    def parent_method(self):
    print('This is a parent method')


    class Child_Class(Parent_Class):

    def __init__(self):
    super(Child_Class, self).__init__()

    def child_method(self):
    # Because of super, self of child class is aware of all its parent's methods and properties.
    self.parent_method()





    share|improve this answer
























    • Thank you for the explanation but may I ask? What about if we have a class from another file and one class in another file? Aint just using import will import everything including the functions and attributes from first file? Example, from fileA import A within "file B". From there, we can use x = A.x or A.somefunction(). So what about this? Is the super does something different or extra on top of import when involving two different files with separate classes. From many examples, I seen all classes are within the same python file and not different file.

      – ohmygodiamnewbie
      Nov 20 '18 at 2:25











    • Also, isnt it just good enough to put super in Child_Class only? Why also in Parent_Class? Sorry I am still learning so I wanted to clear my misunderstanding

      – ohmygodiamnewbie
      Nov 20 '18 at 2:26






    • 1





      Import and super are two different things altogether. Please read and understand inheritance and some basics of OOPS. There are plenty of resources available.

      – Pruthvi Kumar
      Nov 20 '18 at 2:28













    • You'll need super in every class. Should you not do this, MRO will hurt you down the line.

      – Pruthvi Kumar
      Nov 20 '18 at 2:33











    • @ohmygodiamnewbie, accept answer if it has helped your understanding. Thanks.

      – Pruthvi Kumar
      Nov 20 '18 at 2:50
















    0














    You'll need to understand a bit of Object-oriented programming principles to understand super.



    In general, when you are importing a module, that module becomes available for your current function/class.



    By using Super you satisfy inheritance; facilitating your current class's this to have references to all attributes of its parent(s). If you are just developing some functions, you will not need super. This is strictly an object-oriented paradigm.



    from xyz import abc

    def some_function():
    # here you can use abc's attributes by explicit invocation.
    x = abc.x

    # Or call functions.
    abc.some_function()


    When you're dealing with classes, super does the magic of facilitating inheritance.



    class Parent_Class(object):

    def __init__(self):
    super(Parent_Class, self).__init__()

    def parent_method(self):
    print('This is a parent method')


    class Child_Class(Parent_Class):

    def __init__(self):
    super(Child_Class, self).__init__()

    def child_method(self):
    # Because of super, self of child class is aware of all its parent's methods and properties.
    self.parent_method()





    share|improve this answer
























    • Thank you for the explanation but may I ask? What about if we have a class from another file and one class in another file? Aint just using import will import everything including the functions and attributes from first file? Example, from fileA import A within "file B". From there, we can use x = A.x or A.somefunction(). So what about this? Is the super does something different or extra on top of import when involving two different files with separate classes. From many examples, I seen all classes are within the same python file and not different file.

      – ohmygodiamnewbie
      Nov 20 '18 at 2:25











    • Also, isnt it just good enough to put super in Child_Class only? Why also in Parent_Class? Sorry I am still learning so I wanted to clear my misunderstanding

      – ohmygodiamnewbie
      Nov 20 '18 at 2:26






    • 1





      Import and super are two different things altogether. Please read and understand inheritance and some basics of OOPS. There are plenty of resources available.

      – Pruthvi Kumar
      Nov 20 '18 at 2:28













    • You'll need super in every class. Should you not do this, MRO will hurt you down the line.

      – Pruthvi Kumar
      Nov 20 '18 at 2:33











    • @ohmygodiamnewbie, accept answer if it has helped your understanding. Thanks.

      – Pruthvi Kumar
      Nov 20 '18 at 2:50














    0












    0








    0







    You'll need to understand a bit of Object-oriented programming principles to understand super.



    In general, when you are importing a module, that module becomes available for your current function/class.



    By using Super you satisfy inheritance; facilitating your current class's this to have references to all attributes of its parent(s). If you are just developing some functions, you will not need super. This is strictly an object-oriented paradigm.



    from xyz import abc

    def some_function():
    # here you can use abc's attributes by explicit invocation.
    x = abc.x

    # Or call functions.
    abc.some_function()


    When you're dealing with classes, super does the magic of facilitating inheritance.



    class Parent_Class(object):

    def __init__(self):
    super(Parent_Class, self).__init__()

    def parent_method(self):
    print('This is a parent method')


    class Child_Class(Parent_Class):

    def __init__(self):
    super(Child_Class, self).__init__()

    def child_method(self):
    # Because of super, self of child class is aware of all its parent's methods and properties.
    self.parent_method()





    share|improve this answer













    You'll need to understand a bit of Object-oriented programming principles to understand super.



    In general, when you are importing a module, that module becomes available for your current function/class.



    By using Super you satisfy inheritance; facilitating your current class's this to have references to all attributes of its parent(s). If you are just developing some functions, you will not need super. This is strictly an object-oriented paradigm.



    from xyz import abc

    def some_function():
    # here you can use abc's attributes by explicit invocation.
    x = abc.x

    # Or call functions.
    abc.some_function()


    When you're dealing with classes, super does the magic of facilitating inheritance.



    class Parent_Class(object):

    def __init__(self):
    super(Parent_Class, self).__init__()

    def parent_method(self):
    print('This is a parent method')


    class Child_Class(Parent_Class):

    def __init__(self):
    super(Child_Class, self).__init__()

    def child_method(self):
    # Because of super, self of child class is aware of all its parent's methods and properties.
    self.parent_method()






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 1:56









    Pruthvi KumarPruthvi Kumar

    58719




    58719













    • Thank you for the explanation but may I ask? What about if we have a class from another file and one class in another file? Aint just using import will import everything including the functions and attributes from first file? Example, from fileA import A within "file B". From there, we can use x = A.x or A.somefunction(). So what about this? Is the super does something different or extra on top of import when involving two different files with separate classes. From many examples, I seen all classes are within the same python file and not different file.

      – ohmygodiamnewbie
      Nov 20 '18 at 2:25











    • Also, isnt it just good enough to put super in Child_Class only? Why also in Parent_Class? Sorry I am still learning so I wanted to clear my misunderstanding

      – ohmygodiamnewbie
      Nov 20 '18 at 2:26






    • 1





      Import and super are two different things altogether. Please read and understand inheritance and some basics of OOPS. There are plenty of resources available.

      – Pruthvi Kumar
      Nov 20 '18 at 2:28













    • You'll need super in every class. Should you not do this, MRO will hurt you down the line.

      – Pruthvi Kumar
      Nov 20 '18 at 2:33











    • @ohmygodiamnewbie, accept answer if it has helped your understanding. Thanks.

      – Pruthvi Kumar
      Nov 20 '18 at 2:50



















    • Thank you for the explanation but may I ask? What about if we have a class from another file and one class in another file? Aint just using import will import everything including the functions and attributes from first file? Example, from fileA import A within "file B". From there, we can use x = A.x or A.somefunction(). So what about this? Is the super does something different or extra on top of import when involving two different files with separate classes. From many examples, I seen all classes are within the same python file and not different file.

      – ohmygodiamnewbie
      Nov 20 '18 at 2:25











    • Also, isnt it just good enough to put super in Child_Class only? Why also in Parent_Class? Sorry I am still learning so I wanted to clear my misunderstanding

      – ohmygodiamnewbie
      Nov 20 '18 at 2:26






    • 1





      Import and super are two different things altogether. Please read and understand inheritance and some basics of OOPS. There are plenty of resources available.

      – Pruthvi Kumar
      Nov 20 '18 at 2:28













    • You'll need super in every class. Should you not do this, MRO will hurt you down the line.

      – Pruthvi Kumar
      Nov 20 '18 at 2:33











    • @ohmygodiamnewbie, accept answer if it has helped your understanding. Thanks.

      – Pruthvi Kumar
      Nov 20 '18 at 2:50

















    Thank you for the explanation but may I ask? What about if we have a class from another file and one class in another file? Aint just using import will import everything including the functions and attributes from first file? Example, from fileA import A within "file B". From there, we can use x = A.x or A.somefunction(). So what about this? Is the super does something different or extra on top of import when involving two different files with separate classes. From many examples, I seen all classes are within the same python file and not different file.

    – ohmygodiamnewbie
    Nov 20 '18 at 2:25





    Thank you for the explanation but may I ask? What about if we have a class from another file and one class in another file? Aint just using import will import everything including the functions and attributes from first file? Example, from fileA import A within "file B". From there, we can use x = A.x or A.somefunction(). So what about this? Is the super does something different or extra on top of import when involving two different files with separate classes. From many examples, I seen all classes are within the same python file and not different file.

    – ohmygodiamnewbie
    Nov 20 '18 at 2:25













    Also, isnt it just good enough to put super in Child_Class only? Why also in Parent_Class? Sorry I am still learning so I wanted to clear my misunderstanding

    – ohmygodiamnewbie
    Nov 20 '18 at 2:26





    Also, isnt it just good enough to put super in Child_Class only? Why also in Parent_Class? Sorry I am still learning so I wanted to clear my misunderstanding

    – ohmygodiamnewbie
    Nov 20 '18 at 2:26




    1




    1





    Import and super are two different things altogether. Please read and understand inheritance and some basics of OOPS. There are plenty of resources available.

    – Pruthvi Kumar
    Nov 20 '18 at 2:28







    Import and super are two different things altogether. Please read and understand inheritance and some basics of OOPS. There are plenty of resources available.

    – Pruthvi Kumar
    Nov 20 '18 at 2:28















    You'll need super in every class. Should you not do this, MRO will hurt you down the line.

    – Pruthvi Kumar
    Nov 20 '18 at 2:33





    You'll need super in every class. Should you not do this, MRO will hurt you down the line.

    – Pruthvi Kumar
    Nov 20 '18 at 2:33













    @ohmygodiamnewbie, accept answer if it has helped your understanding. Thanks.

    – Pruthvi Kumar
    Nov 20 '18 at 2:50





    @ohmygodiamnewbie, accept answer if it has helped your understanding. Thanks.

    – Pruthvi Kumar
    Nov 20 '18 at 2:50













    0














    import is used for importing module
    __init__() is a class method, which is invoked when a new instance of a class is instantiated

    This is 2 different thing



    EDIT: I've made this edit to clarify the answer for you, since I assume you are not familiar with OOP. TL;DR: These two terms are completely different, they relate to two separate things. super() call a class's parent method, while import import a module




    we can still import the module form file B into file A and in file A
    we can still use the methods from the file B in file A right?




    Right.




    what makes it different than super() which calls the methods from file B when used in file A




    First, you only encounter super() in subclass (lets say class B) of a class(lets say class A). You access A's method through B's instance via super(). Let see this example: You have 2 file, A.py and B.py, each define A and B:



    In A.py, you define class A



    class A(object):
    def __init__(self):
    print("Instantiate class A")

    def printA(self):
    print("AAAAAAAAAAAAAA")


    In B.py, you define class B:



    from A import A  # you need this import for `B.py` to know what is `A`

    class B(A): # Mean B inherit A
    def __init__(self):
    super(B, self).__init__() # This line all its parent's method (`__init__()`)

    def printB(self):
    print("BBBBBBBBBBBB")

    super(B, self).printA() # This line all its parent's method (`printA`)

    # And you instantiate a `B` instance, then call a method of it, which call its super method `printA`
    b = B()
    b.printB()


    Output is (note that `init():




    Instantiate class A

    BBBBBBBBBBBB

    AAAAAAAAAAAAAA




    To conclude, I suggest you to read some material about OOP. You will clearly see the differences btw these two things. Good luck. I hope this help!






    share|improve this answer


























    • My same question to the above comment. Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

      – ohmygodiamnewbie
      Nov 20 '18 at 1:31











    • Aint it better to just use from fileA import fileB and inside file A we just use the self.function(which supposed to be in File B) and still running

      – ohmygodiamnewbie
      Nov 20 '18 at 1:32











    • I've made an edit for my answer, please read it and let me know if you still have problem

      – enamoria
      Nov 20 '18 at 4:45
















    0














    import is used for importing module
    __init__() is a class method, which is invoked when a new instance of a class is instantiated

    This is 2 different thing



    EDIT: I've made this edit to clarify the answer for you, since I assume you are not familiar with OOP. TL;DR: These two terms are completely different, they relate to two separate things. super() call a class's parent method, while import import a module




    we can still import the module form file B into file A and in file A
    we can still use the methods from the file B in file A right?




    Right.




    what makes it different than super() which calls the methods from file B when used in file A




    First, you only encounter super() in subclass (lets say class B) of a class(lets say class A). You access A's method through B's instance via super(). Let see this example: You have 2 file, A.py and B.py, each define A and B:



    In A.py, you define class A



    class A(object):
    def __init__(self):
    print("Instantiate class A")

    def printA(self):
    print("AAAAAAAAAAAAAA")


    In B.py, you define class B:



    from A import A  # you need this import for `B.py` to know what is `A`

    class B(A): # Mean B inherit A
    def __init__(self):
    super(B, self).__init__() # This line all its parent's method (`__init__()`)

    def printB(self):
    print("BBBBBBBBBBBB")

    super(B, self).printA() # This line all its parent's method (`printA`)

    # And you instantiate a `B` instance, then call a method of it, which call its super method `printA`
    b = B()
    b.printB()


    Output is (note that `init():




    Instantiate class A

    BBBBBBBBBBBB

    AAAAAAAAAAAAAA




    To conclude, I suggest you to read some material about OOP. You will clearly see the differences btw these two things. Good luck. I hope this help!






    share|improve this answer


























    • My same question to the above comment. Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

      – ohmygodiamnewbie
      Nov 20 '18 at 1:31











    • Aint it better to just use from fileA import fileB and inside file A we just use the self.function(which supposed to be in File B) and still running

      – ohmygodiamnewbie
      Nov 20 '18 at 1:32











    • I've made an edit for my answer, please read it and let me know if you still have problem

      – enamoria
      Nov 20 '18 at 4:45














    0












    0








    0







    import is used for importing module
    __init__() is a class method, which is invoked when a new instance of a class is instantiated

    This is 2 different thing



    EDIT: I've made this edit to clarify the answer for you, since I assume you are not familiar with OOP. TL;DR: These two terms are completely different, they relate to two separate things. super() call a class's parent method, while import import a module




    we can still import the module form file B into file A and in file A
    we can still use the methods from the file B in file A right?




    Right.




    what makes it different than super() which calls the methods from file B when used in file A




    First, you only encounter super() in subclass (lets say class B) of a class(lets say class A). You access A's method through B's instance via super(). Let see this example: You have 2 file, A.py and B.py, each define A and B:



    In A.py, you define class A



    class A(object):
    def __init__(self):
    print("Instantiate class A")

    def printA(self):
    print("AAAAAAAAAAAAAA")


    In B.py, you define class B:



    from A import A  # you need this import for `B.py` to know what is `A`

    class B(A): # Mean B inherit A
    def __init__(self):
    super(B, self).__init__() # This line all its parent's method (`__init__()`)

    def printB(self):
    print("BBBBBBBBBBBB")

    super(B, self).printA() # This line all its parent's method (`printA`)

    # And you instantiate a `B` instance, then call a method of it, which call its super method `printA`
    b = B()
    b.printB()


    Output is (note that `init():




    Instantiate class A

    BBBBBBBBBBBB

    AAAAAAAAAAAAAA




    To conclude, I suggest you to read some material about OOP. You will clearly see the differences btw these two things. Good luck. I hope this help!






    share|improve this answer















    import is used for importing module
    __init__() is a class method, which is invoked when a new instance of a class is instantiated

    This is 2 different thing



    EDIT: I've made this edit to clarify the answer for you, since I assume you are not familiar with OOP. TL;DR: These two terms are completely different, they relate to two separate things. super() call a class's parent method, while import import a module




    we can still import the module form file B into file A and in file A
    we can still use the methods from the file B in file A right?




    Right.




    what makes it different than super() which calls the methods from file B when used in file A




    First, you only encounter super() in subclass (lets say class B) of a class(lets say class A). You access A's method through B's instance via super(). Let see this example: You have 2 file, A.py and B.py, each define A and B:



    In A.py, you define class A



    class A(object):
    def __init__(self):
    print("Instantiate class A")

    def printA(self):
    print("AAAAAAAAAAAAAA")


    In B.py, you define class B:



    from A import A  # you need this import for `B.py` to know what is `A`

    class B(A): # Mean B inherit A
    def __init__(self):
    super(B, self).__init__() # This line all its parent's method (`__init__()`)

    def printB(self):
    print("BBBBBBBBBBBB")

    super(B, self).printA() # This line all its parent's method (`printA`)

    # And you instantiate a `B` instance, then call a method of it, which call its super method `printA`
    b = B()
    b.printB()


    Output is (note that `init():




    Instantiate class A

    BBBBBBBBBBBB

    AAAAAAAAAAAAAA




    To conclude, I suggest you to read some material about OOP. You will clearly see the differences btw these two things. Good luck. I hope this help!







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 4:44

























    answered Nov 20 '18 at 1:14









    enamoriaenamoria

    602521




    602521













    • My same question to the above comment. Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

      – ohmygodiamnewbie
      Nov 20 '18 at 1:31











    • Aint it better to just use from fileA import fileB and inside file A we just use the self.function(which supposed to be in File B) and still running

      – ohmygodiamnewbie
      Nov 20 '18 at 1:32











    • I've made an edit for my answer, please read it and let me know if you still have problem

      – enamoria
      Nov 20 '18 at 4:45



















    • My same question to the above comment. Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

      – ohmygodiamnewbie
      Nov 20 '18 at 1:31











    • Aint it better to just use from fileA import fileB and inside file A we just use the self.function(which supposed to be in File B) and still running

      – ohmygodiamnewbie
      Nov 20 '18 at 1:32











    • I've made an edit for my answer, please read it and let me know if you still have problem

      – enamoria
      Nov 20 '18 at 4:45

















    My same question to the above comment. Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

    – ohmygodiamnewbie
    Nov 20 '18 at 1:31





    My same question to the above comment. Yea, but from what I understand, we can still import the module form file B into file A and in file A we can still use the methods from the file B in file A right? Then what makes it different than super() which calls the methods from file B when used in file A? Please explain a little if possible.

    – ohmygodiamnewbie
    Nov 20 '18 at 1:31













    Aint it better to just use from fileA import fileB and inside file A we just use the self.function(which supposed to be in File B) and still running

    – ohmygodiamnewbie
    Nov 20 '18 at 1:32





    Aint it better to just use from fileA import fileB and inside file A we just use the self.function(which supposed to be in File B) and still running

    – ohmygodiamnewbie
    Nov 20 '18 at 1:32













    I've made an edit for my answer, please read it and let me know if you still have problem

    – enamoria
    Nov 20 '18 at 4:45





    I've made an edit for my answer, please read it and let me know if you still have problem

    – enamoria
    Nov 20 '18 at 4:45











    0














    The import statement is used to import modules into modules. The super callable is used to delegate from subclass implementation to superclass implementation.



    Notably, import connects between fixed equals whereas super connects in a dynamic hierarchy.





    The point of import is to make a specific module or its content available in another module.



    # moda.py
    def foo(who):
    print(who, "called '%s.foo'" % __name__)

    # modb.py
    import moda
    moda.foo(__name__)


    Note that importing uses a qualified name that uniquely identifies the target. At any time, the name used in an import can refer to only one specific entity. That means that import couples strongly - the target is fully identified and cannot change after the import.





    The point of super is to access the closest superclass implementation in a subclass implementation.



    class A:
    def call(self, who):
    print(who, "called '%s.call'" % __class__, "on self of class", self.__class__)

    class Alpha(A):
    def call(self, who):
    super(Alpha, self).call(who)
    print("... via '%s.call'" % __class__)


    Note that super only work with local information - super does only identify the source, not the target. As such, the target is dynamically bound and not uniquely identifiable. That means that super couples weakly - the target is relatively defined and can only be resolved at runtime.



    class Alpher(A):
    def call(self, who):
    print("'%s.call' sneakily intercepts the call..." % __class__)
    super(Alpher, self).call(who)

    class Better(Alpha, Alpher):
    pass


    The above injects Alpher into the relation from Alpha to A without modifying either of the two.



    >>> Better().call('Saul')
    '<class '__main__.Alpher'>.call' sneakily intercepts the call...
    Saul called <class '__main__.A'>.call on self of class <class '__main__.Better'>
    ...via '<class '__main__.Alpha'>.call'





    share|improve this answer




























      0














      The import statement is used to import modules into modules. The super callable is used to delegate from subclass implementation to superclass implementation.



      Notably, import connects between fixed equals whereas super connects in a dynamic hierarchy.





      The point of import is to make a specific module or its content available in another module.



      # moda.py
      def foo(who):
      print(who, "called '%s.foo'" % __name__)

      # modb.py
      import moda
      moda.foo(__name__)


      Note that importing uses a qualified name that uniquely identifies the target. At any time, the name used in an import can refer to only one specific entity. That means that import couples strongly - the target is fully identified and cannot change after the import.





      The point of super is to access the closest superclass implementation in a subclass implementation.



      class A:
      def call(self, who):
      print(who, "called '%s.call'" % __class__, "on self of class", self.__class__)

      class Alpha(A):
      def call(self, who):
      super(Alpha, self).call(who)
      print("... via '%s.call'" % __class__)


      Note that super only work with local information - super does only identify the source, not the target. As such, the target is dynamically bound and not uniquely identifiable. That means that super couples weakly - the target is relatively defined and can only be resolved at runtime.



      class Alpher(A):
      def call(self, who):
      print("'%s.call' sneakily intercepts the call..." % __class__)
      super(Alpher, self).call(who)

      class Better(Alpha, Alpher):
      pass


      The above injects Alpher into the relation from Alpha to A without modifying either of the two.



      >>> Better().call('Saul')
      '<class '__main__.Alpher'>.call' sneakily intercepts the call...
      Saul called <class '__main__.A'>.call on self of class <class '__main__.Better'>
      ...via '<class '__main__.Alpha'>.call'





      share|improve this answer


























        0












        0








        0







        The import statement is used to import modules into modules. The super callable is used to delegate from subclass implementation to superclass implementation.



        Notably, import connects between fixed equals whereas super connects in a dynamic hierarchy.





        The point of import is to make a specific module or its content available in another module.



        # moda.py
        def foo(who):
        print(who, "called '%s.foo'" % __name__)

        # modb.py
        import moda
        moda.foo(__name__)


        Note that importing uses a qualified name that uniquely identifies the target. At any time, the name used in an import can refer to only one specific entity. That means that import couples strongly - the target is fully identified and cannot change after the import.





        The point of super is to access the closest superclass implementation in a subclass implementation.



        class A:
        def call(self, who):
        print(who, "called '%s.call'" % __class__, "on self of class", self.__class__)

        class Alpha(A):
        def call(self, who):
        super(Alpha, self).call(who)
        print("... via '%s.call'" % __class__)


        Note that super only work with local information - super does only identify the source, not the target. As such, the target is dynamically bound and not uniquely identifiable. That means that super couples weakly - the target is relatively defined and can only be resolved at runtime.



        class Alpher(A):
        def call(self, who):
        print("'%s.call' sneakily intercepts the call..." % __class__)
        super(Alpher, self).call(who)

        class Better(Alpha, Alpher):
        pass


        The above injects Alpher into the relation from Alpha to A without modifying either of the two.



        >>> Better().call('Saul')
        '<class '__main__.Alpher'>.call' sneakily intercepts the call...
        Saul called <class '__main__.A'>.call on self of class <class '__main__.Better'>
        ...via '<class '__main__.Alpha'>.call'





        share|improve this answer













        The import statement is used to import modules into modules. The super callable is used to delegate from subclass implementation to superclass implementation.



        Notably, import connects between fixed equals whereas super connects in a dynamic hierarchy.





        The point of import is to make a specific module or its content available in another module.



        # moda.py
        def foo(who):
        print(who, "called '%s.foo'" % __name__)

        # modb.py
        import moda
        moda.foo(__name__)


        Note that importing uses a qualified name that uniquely identifies the target. At any time, the name used in an import can refer to only one specific entity. That means that import couples strongly - the target is fully identified and cannot change after the import.





        The point of super is to access the closest superclass implementation in a subclass implementation.



        class A:
        def call(self, who):
        print(who, "called '%s.call'" % __class__, "on self of class", self.__class__)

        class Alpha(A):
        def call(self, who):
        super(Alpha, self).call(who)
        print("... via '%s.call'" % __class__)


        Note that super only work with local information - super does only identify the source, not the target. As such, the target is dynamically bound and not uniquely identifiable. That means that super couples weakly - the target is relatively defined and can only be resolved at runtime.



        class Alpher(A):
        def call(self, who):
        print("'%s.call' sneakily intercepts the call..." % __class__)
        super(Alpher, self).call(who)

        class Better(Alpha, Alpher):
        pass


        The above injects Alpher into the relation from Alpha to A without modifying either of the two.



        >>> Better().call('Saul')
        '<class '__main__.Alpher'>.call' sneakily intercepts the call...
        Saul called <class '__main__.A'>.call on self of class <class '__main__.Better'>
        ...via '<class '__main__.Alpha'>.call'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 6:46









        MisterMiyagiMisterMiyagi

        7,5532142




        7,5532142






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384733%2fsuper-and-import-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory