Getting module path string from a class instance

Discussion in 'Python' started by Some Developer, Nov 13, 2012.

  1. 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?
     
    Some Developer, Nov 13, 2012
    #1
    1. Advertising

  2. On Tue, 13 Nov 2012 06:38:31 +0000, 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?


    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
     
    Steven D'Aprano, Nov 13, 2012
    #2
    1. Advertising

  3. On 13/11/2012 07:19, Steven D'Aprano wrote:
    > On Tue, 13 Nov 2012 06:38:31 +0000, 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?

    > 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'
    >
    >

    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.
     
    Some Developer, Nov 13, 2012
    #3
  4. On Tue, 13 Nov 2012 07:54:32 +0000, Some Developer wrote:

    > On 13/11/2012 07:19, Steven D'Aprano wrote:
    >> On Tue, 13 Nov 2012 06:38:31 +0000, 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?

    >> 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'
    >>
    >>

    > 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.


    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
     
    Steven D'Aprano, Nov 13, 2012
    #4
  5. On 13/11/2012 08:49, Steven D'Aprano wrote:
    > On Tue, 13 Nov 2012 07:54:32 +0000, Some Developer wrote:
    >
    >> On 13/11/2012 07:19, Steven D'Aprano wrote:
    >>> On Tue, 13 Nov 2012 06:38:31 +0000, 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?
    >>> 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'
    >>>
    >>>

    >> 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.

    > 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__
    >


    Ah, my mistake. Thanks. That sounds exactly like what I want.

    >> 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.
     
    Some Developer, Nov 13, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Madni
    Replies:
    1
    Views:
    5,924
  2. Sridhar R
    Replies:
    14
    Views:
    1,446
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. gentlestone
    Replies:
    6
    Views:
    984
    Bruno Desthuilliers
    Oct 6, 2009
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    383
    Martin P. Hellwig
    Mar 26, 2010
  5. Some Developer
    Replies:
    0
    Views:
    208
    Some Developer
    Nov 13, 2012
Loading...

Share This Page