Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting module path string from a class instance

38 views
Skip to first unread message

Some Developer

unread,
Nov 13, 2012, 1:38:31 AM11/13/12
to pytho...@python.org
I'm trying to find a way to get a string of the module path of a class.

So for instance say I have class Foo and it is in a module called
my.module. I want to be able to get a string that is equal to this:
"my.module.Foo". I'm aware of the __repr__ method but it does not do
what I want it to do in this case.

Can anyone offer any advice at all?

Steven D'Aprano

unread,
Nov 13, 2012, 2:19:10 AM11/13/12
to
py> from multiprocessing.pool import Pool
py> repr(Pool)
"<class 'multiprocessing.pool.Pool'>"

Seems pretty close to what you ask for. You can either pull that string
apart:

py> s = repr(Pool)
py> start = s.find("'")
py> end = s.rfind("'")
py> s[start+1:end]
'multiprocessing.pool.Pool'

or you can construct it yourself:

py> Pool.__module__ + '.' + Pool.__name__
'multiprocessing.pool.Pool'


--
Steven

Some Developer

unread,
Nov 13, 2012, 2:54:32 AM11/13/12
to pytho...@python.org
Yeah I considered doing it this way but was wary of that method because
of possible changes to the implementation of the __repr__ method in the
upstream code. If the Django developers don't consider the __repr__
method a public API then it could change in the future breaking my code.

Of course this might not happen but I was hoping that there was a more
generic way of doing it that did not rely on a certain implementation
being in existence.

Steven D'Aprano

unread,
Nov 13, 2012, 3:49:42 AM11/13/12
to
I didn't call SomeClass.__repr__. That is an implementation detail of
SomeClass, and could change.

I called repr(SomeClass), which calls the *metaclass* __repr__. That is
less likely to change, although not impossible.


If you're worried, just use the second way:

SomeClass.__module__ + '.' + SomeClass.__name__



> Of course this might not happen but I was hoping that there was a more
> generic way of doing it that did not rely on a certain implementation
> being in existence.

SomeClass.__name__ is the official way to get the name of a class;
SomeClass.__module__ is the official way to get the name of the module or
package it comes from.


--
Steven

Dave Angel

unread,
Nov 13, 2012, 4:36:30 AM11/13/12
to Some Developer, pytho...@python.org
On 11/13/2012 01:38 AM, Some Developer wrote:
> I'm trying to find a way to get a string of the module path of a class.
>
> So for instance say I have class Foo and it is in a module called
> my.module. I want to be able to get a string that is equal to this:
> "my.module.Foo". I'm aware of the __repr__ method but it does not do
> what I want it to do in this case.
>
> Can anyone offer any advice at all?

So you have:

import my.module
theclass = my.module.Foo
print tellme(theClass)

and you want to know how to write tellme? Why not just change it to
take a string, and pass it "my.module.Foo" ?

If you have stored the class away somewhere, and want to figure it out
from there, you could look at the __module__ attribute. that'll tell
you the module name, but not the class name.


If you're really asking how to get that string from an INSTANCE of the
class, then try the __class__ attribute of that instance.




--

DaveA

Some Developer

unread,
Nov 13, 2012, 11:15:13 AM11/13/12
to pytho...@python.org
Ah, my mistake. Thanks. That sounds exactly like what I want.

Some Developer

unread,
Nov 13, 2012, 11:23:12 AM11/13/12
to pytho...@python.org
On 13/11/2012 09:36, Dave Angel wrote:
> On 11/13/2012 01:38 AM, Some Developer wrote:
>> I'm trying to find a way to get a string of the module path of a class.
>>
>> So for instance say I have class Foo and it is in a module called
>> my.module. I want to be able to get a string that is equal to this:
>> "my.module.Foo". I'm aware of the __repr__ method but it does not do
>> what I want it to do in this case.
>>
>> Can anyone offer any advice at all?
> So you have:
>
> import my.module
> theclass = my.module.Foo
> print tellme(theClass)
>
> and you want to know how to write tellme? Why not just change it to
> take a string, and pass it "my.module.Foo" ?
>
> If you have stored the class away somewhere, and want to figure it out
> from there, you could look at the __module__ attribute. that'll tell
> you the module name, but not the class name.
>
>
> If you're really asking how to get that string from an INSTANCE of the
> class, then try the __class__ attribute of that instance.
I'm actually writing a dynamic importer.

Basically when the program starts it queries all the loaded modules for
a certain package. It then stores the module and class information in a
database and then when a certain view is called the specific data
required is loaded by calling importlib.import_module('module.class').
This then populates the view with the required data.

Basically the idea is to be able to display any data required without
having to tightly couple the view code to a specific model type.

Thanks for the help.
0 new messages