Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python have an equivalent to Java Class.forName()?

I have the need to take a string argument and create an object of the class named in that string in Python. In Java, I would use Class.forName().newInstance(). Is there an equivalent in Python?


Thanks for the responses. To answer those who want to know what I'm doing: I want to use a command line argument as the class name, and instantiate it. I'm actually programming in Jython and instantiating Java classes, hence the Java-ness of the question. getattr() works great. Thanks much.

like image 722
Jason Avatar asked Jan 17 '09 08:01

Jason


People also ask

What is class forName () method in Java?

forName(String name, boolean initialize, ClassLoader loader) method returns the Class object associated with the class or interface with the given string name, using the given class loader. The specified class loader is used to load the class or interface.

What is purpose of class forName () method JDBC?

The Class. forName is used to load any given class (within double quotes as String) at run time. For example, when we use IDE, we see there will be a GUI builder which allows us to drag and drop the buttons, text fields, etc. This drag and drop mechanism internally requires certain classes to be loaded at run time.

How do you create an object using class forName in Java?

You can use Class. forName() to get a Class object of the desired class. Then use getConstructor() to find the desired Constructor object. Finally, call newInstance() on that object to get your new instance.


1 Answers

Reflection in python is a lot easier and far more flexible than it is in Java.

I recommend reading this tutorial

There's no direct function (that I know of) which takes a fully qualified class name and returns the class, however you have all the pieces needed to build that, and you can connect them together.

One bit of advice though: don't try to program in Java style when you're in python.

If you can explain what is it that you're trying to do, maybe we can help you find a more pythonic way of doing it.

Here's a function that does what you want:

def get_class( kls ):     parts = kls.split('.')     module = ".".join(parts[:-1])     m = __import__( module )     for comp in parts[1:]:         m = getattr(m, comp)                 return m 

You can use the return value of this function as if it were the class itself.

Here's a usage example:

>>> D = get_class("datetime.datetime") >>> D <type 'datetime.datetime'> >>> D.now() datetime.datetime(2009, 1, 17, 2, 15, 58, 883000) >>> a = D( 2010, 4, 22 ) >>> a datetime.datetime(2010, 4, 22, 0, 0) >>>  

How does that work?

We're using __import__ to import the module that holds the class, which required that we first extract the module name from the fully qualified name. Then we import the module:

m = __import__( module ) 

In this case, m will only refer to the top level module,

For example, if your class lives in foo.baz module, then m will be the module foo
We can easily obtain a reference to foo.baz using getattr( m, 'baz' )

To get from the top level module to the class, have to recursively use gettatr on the parts of the class name

Say for example, if you class name is foo.baz.bar.Model then we do this:

m = __import__( "foo.baz.bar" ) #m is package foo m = getattr( m, "baz" ) #m is package baz m = getattr( m, "bar" ) #m is module bar m = getattr( m, "Model" ) #m is class Model 

This is what's happening in this loop:

for comp in parts[1:]:     m = getattr(m, comp)     

At the end of the loop, m will be a reference to the class. This means that m is actually the class itslef, you can do for instance:

a = m() #instantiate a new instance of the class     b = m( arg1, arg2 ) # pass arguments to the constructor 
like image 82
hasen Avatar answered Sep 18 '22 19:09

hasen