Instantiating classes on the fly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vishalsethia
    New Member
    • Feb 2007
    • 7

    #1

    Instantiating classes on the fly

    Is there a way in which classes can be instantiated on the fly. I would be passed the name of the file to be instantiated through command line which I store in a variable using getopt.

    So the file that needs to be instantiated would be dynamic. How would I achieve that in Python

    Thanks
    --V
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by vishalsethia
    Is there a way in which classes can be instantiated on the fly. I would be passed the name of the file to be instantiated through command line which I store in a variable using getopt.

    So the file that needs to be instantiated would be dynamic. How would I achieve that in Python

    Thanks
    --V
    Hmmm... Maybe you mean instantiate objects on the fly... Anyway, that's what normally happens (on both cases)... To create a file object in your script from a command-line argument, use something like this:

    Code:
    import sys
    # open a file in text mode for reading (create file object)
    try:
        fileObj = open(sys.argv[1])
        # print each line as an example of doing something with a file object
        for line in fileObj:
            print line
        fileObj.close()
    except (IOError, IndexError):
        print "No such file"

    Comment

    • vishalsethia
      New Member
      • Feb 2007
      • 7

      #3
      Sorry for not being clear on the first post. What I meant was

      Script 1
      ---------------
      ./script1.py -f script2.py

      To instantiate script2 , one would normally use in script1

      object = script2.script2 ()
      object.function 1()

      But if the name of script2 is dynamic (which comes from command line), how do I instantiate that object.


      Script 2 (script2.py)
      ------------
      Code:
      class script2:
             def __init__(self):
                    .........
                    print "In script 2 "
             def function1(self):
                    print "Function1 "
      Thanks
      Last edited by bartonc; Feb 6 '07, 09:34 PM. Reason: added [code][/code] tags

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by vishalsethia
        Sorry for not being clear on the first post. What I meant was

        Script 1
        ---------------
        ./script1.py -f script2.py

        To instantiate script2 , one would normally use in script1

        object = script2.script2 ()
        object.function 1()

        But if the name of script2 is dynamic (which comes from command line), how do I instantiate that object.


        Script 2 (script2.py)
        ------------
        Code:
        class script2:
               def __init__(self):
                      .........
                      print "In script 2 "
               def function1(self):
                      print "Function1 "
        Thanks
        Code:
        __import__(filename) # leave off the .py

        Comment

        • vishalsethia
          New Member
          • Feb 2007
          • 7

          #5
          Thanks Barton, That helped me import the module but now how do I instantiate it or access the functions declared within that class.

          Thanks
          --V

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Barton! This sounds like a perfect oportunity for abstract classes! I've never done anything with python and inheritance, however, so I would appreciate your input.

            Comment

            • vishalsethia
              New Member
              • Feb 2007
              • 7

              #7
              Was googling around and found a way to instantiate objects on the fly.

              Script 1 (Arbitrary.py)
              --------------
              Code:
              import string
              class ArbitraryClass:
                      def getClass(self, moduleName):
                              return __import__(
                                      moduleName,
                                      {},
                                      {},
                                      []
                                      )
              In your script which would instantiate other objects
              Script2
              ------------
              Code:
              arb = Arbitrary.ArbitraryClass()
              instance = getattr( arb.getClass("FILENAME"), "CLASSNAME")()
              instance.any_function in that class
              where FILENAME is without .py extension and CLASSNAME is the name of the class that is defined within that FILE.

              Thanks
              Last edited by bartonc; Feb 7 '07, 12:53 AM. Reason: added [code][/code] tags

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Glad to see you've gotten it sorted out. Thanks for posting back the answer.

                Comment

                Working...