Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get FULL type name in python?

Tags:

python

Printing result of type() in Python sometimes reveals type name that is not in current scope. For example, following code will display "<type 'frame'>"

import inspect
a = inspect.currentframe()
print( type( a ) )

But there is no frame type in current scope! If i try to use it in interactive interpreter, i get an error:

>>> frame
NameError: name 'frame' is not defined

So is it any way to get a "fully qualified" type name like "inspect.something.frame" so i can refer it in my code?

UPDATE

Unfortunately, __module__ is not working too:

>>> type( a ).__module__
'__builtin__'
like image 632
grigoryvp Avatar asked Mar 01 '13 19:03

grigoryvp


People also ask

How do you get type in Python?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

What is __ name __ in Python?

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.

How do I print a Python module name?

A module can find out its own module name by looking at the predefined global variable __name__.

What does __ class __ mean in Python?

__class__ is an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: <class 'int'> b. __class__ # Output: <class 'float'> After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .


2 Answers

"What's in a name? That which we call a frame 
 By any other name would smell as sweet."

If you are looking for the Python object that corresponds to the type of a frame, you could use:

In [38]: import types

In [39]: types.FrameType
Out[39]: <type 'frame'>

Of course, this is just a different way of writing type(a):

In [42]: import inspect

In [43]: a = inspect.currentframe()

In [44]: types.FrameType == type(a)
Out[44]: True

If you look inside the types module, you'll find that types.FrameType is defined this way:

try:
    raise TypeError
except TypeError:
    tb = sys.exc_info()[2]
    TracebackType = type(tb)
    FrameType = type(tb.tb_frame)
    del tb

All they are doing is finding an instance of frame and defining FrameType to be the type of this instance.

That is essentially what you are doing when you define

MyFrameType = type(a)

So the conclusion is: to get your hands on the type of an instance, just call type(obj). You don't need to know aprior anything else.

like image 136
unutbu Avatar answered Nov 03 '22 14:11

unutbu


There's no simple way to do this. Generally, you can just use:

x.__module__ + '.' + x.__name__

However, this won't work if the object isn't accessible from its module; for example, this can happen if the module uses del to remove the object from its namespace.

For class methods, PEP 3155 introduces the __qualname__ attribute; but that also won't help in this case. type(a).__module__ is '__builtin__', but frame is not a name in the __builtin__ module. This is fairly common with types that are implemented in C.

In this case, you have to know that the frame type is in the types module:

from types import FrameType as frame
like image 45
ecatmur Avatar answered Nov 03 '22 14:11

ecatmur