2

I want to access module-level variables of the module that defines the class the instance is derived from. I only have the instance to work from. I tried self.__class__.__module__[<some var>] but unlike the __class__ attribute which returns the class object, __module__ just returns a string name, not the module object itself. How can I get the module object in this situation?

1 Answer 1

3

The __module__ attribute can be used as a key in the sys.modules dictionary:

import sys
class_module = sys.modules[instance.__class__.__module__]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.