Python : How to call a class methods(parent) in another class methods(child) only by importing class files(parent to child)?










1















I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.



so far i did below approach,
base class
import sqlite3
from sqlite3 import Error



class LocalDb:
def insert_method():
pass


have import insert_method to below class.
from access_db.local_db.local_db import LocalDb



class Main():

def __init__(self):
pass

def change(self):
LocalDb.iterate_db()

if __name__ == '__main__':
main = Main()
main.change()


when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"



also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.



here my questios are,



  1. how to import class A methods in class B and how to call class A methods in class B?


  2. if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?


  3. or am i getting confuse with inheritance and importing class methods to one class? or both are same?


  4. even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?


please some one help me on this.










share|improve this question




























    1















    I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.



    so far i did below approach,
    base class
    import sqlite3
    from sqlite3 import Error



    class LocalDb:
    def insert_method():
    pass


    have import insert_method to below class.
    from access_db.local_db.local_db import LocalDb



    class Main():

    def __init__(self):
    pass

    def change(self):
    LocalDb.iterate_db()

    if __name__ == '__main__':
    main = Main()
    main.change()


    when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"



    also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.



    here my questios are,



    1. how to import class A methods in class B and how to call class A methods in class B?


    2. if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?


    3. or am i getting confuse with inheritance and importing class methods to one class? or both are same?


    4. even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?


    please some one help me on this.










    share|improve this question


























      1












      1








      1








      I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.



      so far i did below approach,
      base class
      import sqlite3
      from sqlite3 import Error



      class LocalDb:
      def insert_method():
      pass


      have import insert_method to below class.
      from access_db.local_db.local_db import LocalDb



      class Main():

      def __init__(self):
      pass

      def change(self):
      LocalDb.iterate_db()

      if __name__ == '__main__':
      main = Main()
      main.change()


      when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"



      also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.



      here my questios are,



      1. how to import class A methods in class B and how to call class A methods in class B?


      2. if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?


      3. or am i getting confuse with inheritance and importing class methods to one class? or both are same?


      4. even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?


      please some one help me on this.










      share|improve this question
















      I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.



      so far i did below approach,
      base class
      import sqlite3
      from sqlite3 import Error



      class LocalDb:
      def insert_method():
      pass


      have import insert_method to below class.
      from access_db.local_db.local_db import LocalDb



      class Main():

      def __init__(self):
      pass

      def change(self):
      LocalDb.iterate_db()

      if __name__ == '__main__':
      main = Main()
      main.change()


      when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"



      also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.



      here my questios are,



      1. how to import class A methods in class B and how to call class A methods in class B?


      2. if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?


      3. or am i getting confuse with inheritance and importing class methods to one class? or both are same?


      4. even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?


      please some one help me on this.







      database python-2.7 inheritance sqlite3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 6:37







      Santhosh

















      asked Nov 14 '18 at 6:20









      SanthoshSanthosh

      146




      146






















          1 Answer
          1






          active

          oldest

          votes


















          1














          although your method does not get self as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.



          what you need here is a static method (assuming you do not need an instance of the LocalDb class available.



          1. use @staticmethod decorator - for docomentation.
            import the class with from filename import LocalDb and its methods will come with it.


          2. irrelevent if we are using a static method.


          3. irrelevent if we are using a static method.

          4. for general knowledge here is the documentation of super - link, it is used to access the inherited class.

          in summery the implementation of your code would look something like this:



          class LocalDb:
          @staticmethod
          def insert_method():
          pass





          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%2f53294192%2fpython-how-to-call-a-class-methodsparent-in-another-class-methodschild-onl%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            although your method does not get self as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.



            what you need here is a static method (assuming you do not need an instance of the LocalDb class available.



            1. use @staticmethod decorator - for docomentation.
              import the class with from filename import LocalDb and its methods will come with it.


            2. irrelevent if we are using a static method.


            3. irrelevent if we are using a static method.

            4. for general knowledge here is the documentation of super - link, it is used to access the inherited class.

            in summery the implementation of your code would look something like this:



            class LocalDb:
            @staticmethod
            def insert_method():
            pass





            share|improve this answer





























              1














              although your method does not get self as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.



              what you need here is a static method (assuming you do not need an instance of the LocalDb class available.



              1. use @staticmethod decorator - for docomentation.
                import the class with from filename import LocalDb and its methods will come with it.


              2. irrelevent if we are using a static method.


              3. irrelevent if we are using a static method.

              4. for general knowledge here is the documentation of super - link, it is used to access the inherited class.

              in summery the implementation of your code would look something like this:



              class LocalDb:
              @staticmethod
              def insert_method():
              pass





              share|improve this answer



























                1












                1








                1







                although your method does not get self as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.



                what you need here is a static method (assuming you do not need an instance of the LocalDb class available.



                1. use @staticmethod decorator - for docomentation.
                  import the class with from filename import LocalDb and its methods will come with it.


                2. irrelevent if we are using a static method.


                3. irrelevent if we are using a static method.

                4. for general knowledge here is the documentation of super - link, it is used to access the inherited class.

                in summery the implementation of your code would look something like this:



                class LocalDb:
                @staticmethod
                def insert_method():
                pass





                share|improve this answer















                although your method does not get self as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.



                what you need here is a static method (assuming you do not need an instance of the LocalDb class available.



                1. use @staticmethod decorator - for docomentation.
                  import the class with from filename import LocalDb and its methods will come with it.


                2. irrelevent if we are using a static method.


                3. irrelevent if we are using a static method.

                4. for general knowledge here is the documentation of super - link, it is used to access the inherited class.

                in summery the implementation of your code would look something like this:



                class LocalDb:
                @staticmethod
                def insert_method():
                pass






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 11:02

























                answered Nov 14 '18 at 10:53









                Omer Ben HaimOmer Ben Haim

                1467




                1467





























                    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%2f53294192%2fpython-how-to-call-a-class-methodsparent-in-another-class-methodschild-onl%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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto