Search notes:

Python: type

type(obj) returns the type of obj. The returned value is an object whose type is type (that is: type(type(x)) is type).
#!/usr/bin/python3

print ( type( []                      ))  # <class 'list'>
print ( type( ['foo', 1 ]             ))  # <class 'list'>
print ( type( {}                      ))  # <class 'dict'>
print ( type( ''                      ))  # <class 'str'>
print ( type( 1 == 0                  ))  # <class 'bool'>
print ( type( True                    ))  # <class 'bool'>
print ( type( str  ('foo')            ))  # <class 'str'>
print ( type( bytes('foo', 'utf-8')   ))  # <class 'bytes'>
print ( type( 'bar'                   ))  # <class 'str'>
print ( type(  0                      ))  # <class 'int'>
print ( type(  42                     ))  # <class 'int'>
print ( type(  42.42                  ))  # <class 'float'>
print ( type(  None                   ))  # <class 'NoneType'>
print ( type( lambda a: a             ))  # <class 'function'>
print ( type( type( 0 )               ))  # <class 'type'>
print ( type( list                    ))  # <class 'type'>
print ( type( dict                    ))  # <class 'type'>
print ( type( type.__dict__           ))  # <class 'mappingproxy'>

print ("---")

def   f(): pass
print ( type( f.__code__              ))  # <class 'code'>

async def a(): pass
aa = a()
print ( type(aa                       ))  # <class 'coroutine'>

class class_A():
      def method(self): pass
obj_a = class_A()
print ( type(class_A                  ))  # <class 'type'>
print ( type(obj_a.method             ))  # <class 'method'>


print ( type(obj_a                    ))  # <class '__main__.class_A'>

print ( type(obj_a) is class_A         )  # True

class class_B(class_A): pass

instance_B=class_B();
print ( type(instance_B) is class_B    )  # True
print ( type(instance_B) is class_A    )  # False
print ( isinstance(instance_B, class_A))  # True
print ( isinstance(instance_B, class_B))  # True

print ( type(  print                  ))  # <class 'builtin_function_or_method'>

import sys
print ( type( sys.implementation      ))  # <class 'types.SimpleNamespace'>
Github repository about-python, path: /built-in/functions/type.py

See also

Every object has a type
isinstance() vs type()
Types
The dunders
Python: Built in functions

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758192482, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/built-in-functions/type/index(107): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78