Mailing List Archive

Getty fully qualified class name from class object
How can I programmatically get the fully qualified name of a class from
its class object? (I'm referring to the name that is shown when str()
or repr() is called on the class object.)

Neither the __name__ or __qualname__ class attributes include the
module. For example:

>>> import logging

>>> str(logging.Handler)
"<class 'logging.Handler'>"

>>> logging.Handler.__name__
'Handler'
>>> logging.Handler.__qualname__
'Handler'

How can I programmatically get 'logging.Handler' from the class object?

--
========================================================================
Google Where SkyNet meets Idiocracy
========================================================================
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getty fully qualified class name from class object [ In reply to ]
On 23/08/23 2:45 am, Ian Pilcher wrote:
> How can I programmatically get 'logging.Handler' from the class object?

Classes have a __module__ attribute:

>>> logging.Handler.__module__
'logging'

--
Greg


--
https://mail.python.org/mailman/listinfo/python-list
Re: Getty fully qualified class name from class object [ In reply to ]
On 8/22/23 11:13, Greg Ewing via Python-list wrote:
> Classes have a __module__ attribute:
>
> >>> logging.Handler.__module__
> 'logging'

Not sure why I didn't think to look for such a thing. Looks like it's
as simple as f'{cls.__module__}.{cls.__qualname__}'.

Thanks!

--
========================================================================
Google Where SkyNet meets Idiocracy
========================================================================

--
https://mail.python.org/mailman/listinfo/python-list