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

Time:04-29

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

CodePudding user response:

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

CodePudding user response:

The class name:

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

The module name:

>>> t.__module__
'torch'
  • Related