Dhara
Dhara

Reputation: 6767

How to get a reference to the module of a class

You can get the name of the module a class belongs to as follows:

myClass.__module__

How do I get a reference to the module instead? More specifically, I want to access the doc-string of the module an arbitrary class is part of. Something similar to:

mod = myClass.getModule() # How do I do this?
print mod.__doc__

Upvotes: 2

Views: 153

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121266

Look the module up in sys.modules:

import sys

mod = sys.modules[cls.__module__]

Demo:

>>> from datetime import datetime
>>> import sys
>>> print sys.modules[datetime.__module__].__doc__
Fast implementation of the datetime type.

Upvotes: 4

Related Questions