is there a way to create a class object or an instance object for a
class if you have the name of the class as a string at the runtime.
For example, in Java you can create a 'Class' object as:
Class.forname ('<classname>')
Thanks.
\vivek
I don't know about Java.... But, from your question, it sounds like you're
asking about an object factory.
def factory(aClass, *args):
return apply(aClass,args)
so if you have a class Spam, that takes 2 args, during runtime, you can
tell it:
newobject=factory(Spam, arg1, arg2)
HTH
Anna
#!/usr/bin/env python
class Junk:
def __init__(self):
print "I am Junk"
s = 'Junk'
j = locals()[s]()
// m
-
class = eval('<classname>')
instance = eval('<classname>')(<constructor_args>)
Cheers,
Brian
Note that this needs aClass to be a class object, not a class name. And
in today's Python, apply's rather redundant -- the body of this factory
function might just as well be:
return aClass(*args)
since the * form can now be applied on calling, symmetrically to its
application in def (it expands any sequence of arguments). So you might
equally well define, in modern Python:
factory = aClass
Alex
In Python, what you load are *modules*, and classes (as well as
everything else) live in modules. This is somewhat different from the
Java approach where classes are "directly loaded".
So, to "load and access" a class from a name, in Python you need
two steps, and two names:
-- a module name, which you will use to load the module (or to
access it if it's already loaded);
-- a class name within the module.
def forname(modname, classname):
module = __import__(modname)
classobj = getattr(module, classname)
return classobj
Note that, while I've used "classname" and "classobj" here, this is
NOT at all limited to classes -- it will access just as well any module
attribute at all, be it a class, a function, a number, whatever.
In practice one might be less didactic and more concise and generic:
def forname(modname, attname):
return getattr(__import__(modname), attname)
Alex
You can use Python's dynamic execution features to do what you want. To
retrieve a reference to an existing class object use the built-in 'eval'
function.
classobj = eval('<classname>')
Create an instance of the class by calling the returned object.
classinstance = classobj(<constructor args>)
To define an entirely new class at run-time use the 'exec' statement. The
following example creates a minimal class, but the definition can be as
complicated as you need.
# Class name is defined by a string.
classname = 'mynewclass'
# The class definition is also a string. The '%s' will be replaced by the
class name later.
# When writing a compound statement as a string make sure the indentation
method
# is consistent, e.g. four spaces. See section 2.4.1 in Python Language
Manual
# for more on triple-quoted strings.
definition = """class %s:
pass
"""
# Execute the class definition statement. Insert the class name using a
# string formatting command '%' (section 2.2.6.2 in Python Library
Reference).
exec definition % classname
# Create an instance of the class.
newinstance = eval(classname)()
# This also works, but only when classname == 'mynewclass'.
newinstance = mynewclass()
> For example, in Java you can create a 'Class' object as:
>
> Class.forname ('<classname>')
>
> Thanks.
>
> \vivek
>
The Java Class.forName method returns a reference to an already existing
'Class' object. It does not create a new 'Class' object. It will not create
a new class at run time.
I hope this helps.
Lenard Lindstrom
For security reasons don't forget to check each input string for eval()
thoroughly.
Ciao, Michael.