:PROPERTIES:
:ID:       a32ab138-f9a8-4d61-9c09-97953c5a0a92
:mtime:    20220516152042
:ctime:    20220516145022
:END:
#+title: type == object


* Introduction
#+BEGIN_SRC python :results output
from termcolor import colored

def check_instance(obj, type_):
    repr_attrs=['bold']
    ret = isinstance(obj, type_)
    print(f"Is {colored(obj.__name__, attrs=repr_attrs)} a instance of {colored(type_.__name__, attrs=repr_attrs)}: {ret}")
    return ret

for obj, type_ in [(type, object), (object, type), (object, object), (type, type)]:
    check_instance(obj, type_)
#+END_SRC

#+RESULTS:
: Is type a instance of object: True
: Is object a instance of type: True
: Is object a instance of object: True
: Is type a instance of type: True

* Explications
 * Tout est objet, ainsi ~isinstance(Anything, object)~ retourne toujours ~True~,
 * /type/ est une /metaclass/ permettant la construction de tous les types : int, str et object sont des instances du
   type /class/,
 * /type/ est le seul objet qui est une instance de lui même.

#+BEGIN_SRC python :results output
print(type(5))
print(type(int))
print(type(type))
#+END_SRC

#+RESULTS:
: <class 'int'>
: <class 'type'>
: <class 'type'>
 
* Références
 * [[https://medium.com/@saint_sdmn/10-hardest-python-questions-98986c8cd309][10 Hardest Python Questions - Medium]]
