'How to get the name of a method's class in Python?

I need a function that gets the name of a method's module, ie:

import torch
t = torch.Tensor(...)# arguments don't matter
foo(t)
>>>torch

I know __name__ exists, but it doesn't do what I need



Solution 1:[1]

To get the name of the module which supports a particular class or function, you need to use the __class__.__module__ properties:

import numpy as np

t = np.matrix([])
print(t.__class__.__module__)

Solution 2:[2]

The class name:

>>> import torch
>>> t = torch.Tensor()
>>> type(t).__name__
'Tensor'

The module name:

>>> t.__module__
'torch'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cardstdani
Solution 2 wim