How import module as class?












-1















I had a problem when I wanted to create my factory.



I have a directory that looks like that:



classes/
file_1.py
file_2.py
....
factory.py


In file_1 and file_2, i have some classes definition in like this:



class A():
...

class B():
...


In factory.py i want create a class on which I could use getattr function.



I tried to do that:



from classes import file_1, file_2

class Factory(file_1, file_2):
"""doctring"""

def build(dic):
factory = Factory()
return getattr(dic['name'], factory)(**dic['params'])


But file_1 and file_2 are module, not a class.



So what are the other ways that I can use to make my factory ?

And if there are several, which is the best ?



Thank you.










share|improve this question





























    -1















    I had a problem when I wanted to create my factory.



    I have a directory that looks like that:



    classes/
    file_1.py
    file_2.py
    ....
    factory.py


    In file_1 and file_2, i have some classes definition in like this:



    class A():
    ...

    class B():
    ...


    In factory.py i want create a class on which I could use getattr function.



    I tried to do that:



    from classes import file_1, file_2

    class Factory(file_1, file_2):
    """doctring"""

    def build(dic):
    factory = Factory()
    return getattr(dic['name'], factory)(**dic['params'])


    But file_1 and file_2 are module, not a class.



    So what are the other ways that I can use to make my factory ?

    And if there are several, which is the best ?



    Thank you.










    share|improve this question



























      -1












      -1








      -1








      I had a problem when I wanted to create my factory.



      I have a directory that looks like that:



      classes/
      file_1.py
      file_2.py
      ....
      factory.py


      In file_1 and file_2, i have some classes definition in like this:



      class A():
      ...

      class B():
      ...


      In factory.py i want create a class on which I could use getattr function.



      I tried to do that:



      from classes import file_1, file_2

      class Factory(file_1, file_2):
      """doctring"""

      def build(dic):
      factory = Factory()
      return getattr(dic['name'], factory)(**dic['params'])


      But file_1 and file_2 are module, not a class.



      So what are the other ways that I can use to make my factory ?

      And if there are several, which is the best ?



      Thank you.










      share|improve this question
















      I had a problem when I wanted to create my factory.



      I have a directory that looks like that:



      classes/
      file_1.py
      file_2.py
      ....
      factory.py


      In file_1 and file_2, i have some classes definition in like this:



      class A():
      ...

      class B():
      ...


      In factory.py i want create a class on which I could use getattr function.



      I tried to do that:



      from classes import file_1, file_2

      class Factory(file_1, file_2):
      """doctring"""

      def build(dic):
      factory = Factory()
      return getattr(dic['name'], factory)(**dic['params'])


      But file_1 and file_2 are module, not a class.



      So what are the other ways that I can use to make my factory ?

      And if there are several, which is the best ?



      Thank you.







      python python-3.x python-import






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 11:28









      petezurich

      3,50581734




      3,50581734










      asked Nov 22 '18 at 10:22









      iEldeniElden

      642317




      642317
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You should do



          from classes.file_1 import class_in_file_1


          Note that if you're using python2, then you need to create __init__.py file in classes directory. No problem if that file is empty






          share|improve this answer
























          • it's not what I want, I have a lot of classes in my files and i want to call it according to his name, but if i do class Factory(A, B, C): it does not put classes in attribute of the Factory object.

            – iElden
            Nov 22 '18 at 10:32








          • 1





            So you want to import all A, B, C so your Factory can inherit all of them? I don't really get your point

            – enamoria
            Nov 22 '18 at 10:36











          • I do not want Factory to inherit from A, B, C, ... I would like A, B, C, ... to be a Factory attribute, as if I had inherited the file

            – iElden
            Nov 22 '18 at 10:39













          • @iElden Could you explain what you mean by "inheriting" a file?

            – Norrius
            Nov 22 '18 at 10:47











          • @Norrius I would like classes that are in my file to work as attributes of file. Which would allow me to use getattr to easily have the class I want.

            – iElden
            Nov 22 '18 at 10:50



















          0














          after some research and discussions, I finally found a solution.



          i added a __init__ in classes/ folder :



          classes/
          __init__.py
          file_1.py
          file_2.py
          ....
          factory.py


          And in this file i added a from classes.file_1 import * for each file.



          So, in factory.py i just do:



          import classes

          def build(dic):
          return getattr(classes, dic['name'])(**dic['params']))


          I do not know if this is the best way but it's work fine.

          Thanks to you two @enamoria and @Norrius






          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%2f53428749%2fhow-import-module-as-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You should do



            from classes.file_1 import class_in_file_1


            Note that if you're using python2, then you need to create __init__.py file in classes directory. No problem if that file is empty






            share|improve this answer
























            • it's not what I want, I have a lot of classes in my files and i want to call it according to his name, but if i do class Factory(A, B, C): it does not put classes in attribute of the Factory object.

              – iElden
              Nov 22 '18 at 10:32








            • 1





              So you want to import all A, B, C so your Factory can inherit all of them? I don't really get your point

              – enamoria
              Nov 22 '18 at 10:36











            • I do not want Factory to inherit from A, B, C, ... I would like A, B, C, ... to be a Factory attribute, as if I had inherited the file

              – iElden
              Nov 22 '18 at 10:39













            • @iElden Could you explain what you mean by "inheriting" a file?

              – Norrius
              Nov 22 '18 at 10:47











            • @Norrius I would like classes that are in my file to work as attributes of file. Which would allow me to use getattr to easily have the class I want.

              – iElden
              Nov 22 '18 at 10:50
















            0














            You should do



            from classes.file_1 import class_in_file_1


            Note that if you're using python2, then you need to create __init__.py file in classes directory. No problem if that file is empty






            share|improve this answer
























            • it's not what I want, I have a lot of classes in my files and i want to call it according to his name, but if i do class Factory(A, B, C): it does not put classes in attribute of the Factory object.

              – iElden
              Nov 22 '18 at 10:32








            • 1





              So you want to import all A, B, C so your Factory can inherit all of them? I don't really get your point

              – enamoria
              Nov 22 '18 at 10:36











            • I do not want Factory to inherit from A, B, C, ... I would like A, B, C, ... to be a Factory attribute, as if I had inherited the file

              – iElden
              Nov 22 '18 at 10:39













            • @iElden Could you explain what you mean by "inheriting" a file?

              – Norrius
              Nov 22 '18 at 10:47











            • @Norrius I would like classes that are in my file to work as attributes of file. Which would allow me to use getattr to easily have the class I want.

              – iElden
              Nov 22 '18 at 10:50














            0












            0








            0







            You should do



            from classes.file_1 import class_in_file_1


            Note that if you're using python2, then you need to create __init__.py file in classes directory. No problem if that file is empty






            share|improve this answer













            You should do



            from classes.file_1 import class_in_file_1


            Note that if you're using python2, then you need to create __init__.py file in classes directory. No problem if that file is empty







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 10:27









            enamoriaenamoria

            602521




            602521













            • it's not what I want, I have a lot of classes in my files and i want to call it according to his name, but if i do class Factory(A, B, C): it does not put classes in attribute of the Factory object.

              – iElden
              Nov 22 '18 at 10:32








            • 1





              So you want to import all A, B, C so your Factory can inherit all of them? I don't really get your point

              – enamoria
              Nov 22 '18 at 10:36











            • I do not want Factory to inherit from A, B, C, ... I would like A, B, C, ... to be a Factory attribute, as if I had inherited the file

              – iElden
              Nov 22 '18 at 10:39













            • @iElden Could you explain what you mean by "inheriting" a file?

              – Norrius
              Nov 22 '18 at 10:47











            • @Norrius I would like classes that are in my file to work as attributes of file. Which would allow me to use getattr to easily have the class I want.

              – iElden
              Nov 22 '18 at 10:50



















            • it's not what I want, I have a lot of classes in my files and i want to call it according to his name, but if i do class Factory(A, B, C): it does not put classes in attribute of the Factory object.

              – iElden
              Nov 22 '18 at 10:32








            • 1





              So you want to import all A, B, C so your Factory can inherit all of them? I don't really get your point

              – enamoria
              Nov 22 '18 at 10:36











            • I do not want Factory to inherit from A, B, C, ... I would like A, B, C, ... to be a Factory attribute, as if I had inherited the file

              – iElden
              Nov 22 '18 at 10:39













            • @iElden Could you explain what you mean by "inheriting" a file?

              – Norrius
              Nov 22 '18 at 10:47











            • @Norrius I would like classes that are in my file to work as attributes of file. Which would allow me to use getattr to easily have the class I want.

              – iElden
              Nov 22 '18 at 10:50

















            it's not what I want, I have a lot of classes in my files and i want to call it according to his name, but if i do class Factory(A, B, C): it does not put classes in attribute of the Factory object.

            – iElden
            Nov 22 '18 at 10:32







            it's not what I want, I have a lot of classes in my files and i want to call it according to his name, but if i do class Factory(A, B, C): it does not put classes in attribute of the Factory object.

            – iElden
            Nov 22 '18 at 10:32






            1




            1





            So you want to import all A, B, C so your Factory can inherit all of them? I don't really get your point

            – enamoria
            Nov 22 '18 at 10:36





            So you want to import all A, B, C so your Factory can inherit all of them? I don't really get your point

            – enamoria
            Nov 22 '18 at 10:36













            I do not want Factory to inherit from A, B, C, ... I would like A, B, C, ... to be a Factory attribute, as if I had inherited the file

            – iElden
            Nov 22 '18 at 10:39







            I do not want Factory to inherit from A, B, C, ... I would like A, B, C, ... to be a Factory attribute, as if I had inherited the file

            – iElden
            Nov 22 '18 at 10:39















            @iElden Could you explain what you mean by "inheriting" a file?

            – Norrius
            Nov 22 '18 at 10:47





            @iElden Could you explain what you mean by "inheriting" a file?

            – Norrius
            Nov 22 '18 at 10:47













            @Norrius I would like classes that are in my file to work as attributes of file. Which would allow me to use getattr to easily have the class I want.

            – iElden
            Nov 22 '18 at 10:50





            @Norrius I would like classes that are in my file to work as attributes of file. Which would allow me to use getattr to easily have the class I want.

            – iElden
            Nov 22 '18 at 10:50













            0














            after some research and discussions, I finally found a solution.



            i added a __init__ in classes/ folder :



            classes/
            __init__.py
            file_1.py
            file_2.py
            ....
            factory.py


            And in this file i added a from classes.file_1 import * for each file.



            So, in factory.py i just do:



            import classes

            def build(dic):
            return getattr(classes, dic['name'])(**dic['params']))


            I do not know if this is the best way but it's work fine.

            Thanks to you two @enamoria and @Norrius






            share|improve this answer




























              0














              after some research and discussions, I finally found a solution.



              i added a __init__ in classes/ folder :



              classes/
              __init__.py
              file_1.py
              file_2.py
              ....
              factory.py


              And in this file i added a from classes.file_1 import * for each file.



              So, in factory.py i just do:



              import classes

              def build(dic):
              return getattr(classes, dic['name'])(**dic['params']))


              I do not know if this is the best way but it's work fine.

              Thanks to you two @enamoria and @Norrius






              share|improve this answer


























                0












                0








                0







                after some research and discussions, I finally found a solution.



                i added a __init__ in classes/ folder :



                classes/
                __init__.py
                file_1.py
                file_2.py
                ....
                factory.py


                And in this file i added a from classes.file_1 import * for each file.



                So, in factory.py i just do:



                import classes

                def build(dic):
                return getattr(classes, dic['name'])(**dic['params']))


                I do not know if this is the best way but it's work fine.

                Thanks to you two @enamoria and @Norrius






                share|improve this answer













                after some research and discussions, I finally found a solution.



                i added a __init__ in classes/ folder :



                classes/
                __init__.py
                file_1.py
                file_2.py
                ....
                factory.py


                And in this file i added a from classes.file_1 import * for each file.



                So, in factory.py i just do:



                import classes

                def build(dic):
                return getattr(classes, dic['name'])(**dic['params']))


                I do not know if this is the best way but it's work fine.

                Thanks to you two @enamoria and @Norrius







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 11:43









                iEldeniElden

                642317




                642317






























                    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%2f53428749%2fhow-import-module-as-class%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python