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__'
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.
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.
A module can find out its own module name by looking at the predefined global variable __name__.
__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 .
"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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With