470,841 Members | 921 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

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
Feb 6 '07 #1
7 2307
bartonc
6,596 Expert 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. # open a file in text mode for reading (create file object)
  3. try:
  4.     fileObj = open(sys.argv[1])
  5.     # print each line as an example of doing something with a file object
  6.     for line in fileObj:
  7.         print line
  8.     fileObj.close()
  9. except (IOError, IndexError):
  10.     print "No such file"
Feb 6 '07 #2
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.function1()

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


Script 2 (script2.py)
------------
Expand|Select|Wrap|Line Numbers
  1. class script2:
  2.        def __init__(self):
  3.               .........
  4.               print "In script 2 "
  5.        def function1(self):
  6.               print "Function1 "
  7.  
Thanks
Feb 6 '07 #3
bartonc
6,596 Expert 4TB
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.function1()

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


Script 2 (script2.py)
------------
Expand|Select|Wrap|Line Numbers
  1. class script2:
  2.        def __init__(self):
  3.               .........
  4.               print "In script 2 "
  5.        def function1(self):
  6.               print "Function1 "
  7.  
Thanks
Expand|Select|Wrap|Line Numbers
  1. __import__(filename) # leave off the .py
Feb 6 '07 #4
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
Feb 6 '07 #5
Motoma
3,237 Expert 2GB
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.
Feb 6 '07 #6
Was googling around and found a way to instantiate objects on the fly.

Script 1 (Arbitrary.py)
--------------
Expand|Select|Wrap|Line Numbers
  1. import string
  2. class ArbitraryClass:
  3.         def getClass(self, moduleName):
  4.                 return __import__(
  5.                         moduleName,
  6.                         {},
  7.                         {},
  8.                         []
  9.                         )
  10.  
In your script which would instantiate other objects
Script2
------------
Expand|Select|Wrap|Line Numbers
  1. arb = Arbitrary.ArbitraryClass()
  2. instance = getattr( arb.getClass("FILENAME"), "CLASSNAME")()
  3. instance.any_function in that class 
  4.  
where FILENAME is without .py extension and CLASSNAME is the name of the class that is defined within that FILE.

Thanks
Feb 6 '07 #7
Motoma
3,237 Expert 2GB
Glad to see you've gotten it sorted out. Thanks for posting back the answer.
Feb 7 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Nischal Topno | last post: by
3 posts views Thread by SammySAm | last post: by
2 posts views Thread by active | last post: by
2 posts views Thread by sifl | last post: by
4 posts views Thread by dascandy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.