Search notes:

Python: Objects

Every object has
An object's identity can be queried with id(obj).
The Identities of two object can be compared with the is operator.
Each object belongs to a module.
The same object might be bound to any number of variables (or more technically: entries in symbol tables)

Every object has a type

Every object has type. This type determines the abilities of an object and by extension what an object represents.
Because in Python a type is also an object, the type of a type is the type type.
The built-in function type(obj) returns the type-object of obj.
def printTypeOfType(obj):

    type_of_obj  = type(obj        )
    type_of_type = type(type_of_obj)

    print(f'obj          = {obj         }')
    print(f'type_of_obj  = {type_of_obj }')
    print(f'type_of_type = {type_of_type}')
    print('')

printTypeOfType( 42          )
#
# obj          = 42
# type_of_obj  = <class 'int'>
# type_of_type = <class 'type'>

printTypeOfType( 99.9        )
#
# obj          = 99.9
# type_of_obj  = <class 'float'>
# type_of_type = <class 'type'>

printTypeOfType('Hello world')
#
# obj          = Hello world
# type_of_obj  = <class 'str'>
# type_of_type = <class 'type'>

printTypeOfType(printTypeOfType)
#
# obj          = <function printTypeOfType at 0x000001F329B07F70>
# type_of_obj  = <class 'function'>
# type_of_type = <class 'type'>
Github repository about-Python, path: /objects/objects-have-a-type.py

Serialization (persistation)

Python objects might be serialized (persisted) with the pickle, shelve or json module.

Mutable vs immutable objects

Python distinguishes between mutable and immutable objects.

Internal C structures and mechanisms

CPython stores a Python object internally in a PyObject or PyVarObject (if variable size) struct (both defined in Include/object.h)
All objects except type objects are always allocated on the heap, never on the stack.
The characteristics of a given type are stored in a PyTypeObject struct.

__dir__

If obj is an object that has a method named __dir__, this method will be called by dir(obj).
This is typically used in conjuction with __getattr__() and/or __getattribute__().

See also

Evaluating objects in boolean contexts, such as if or while statements.
callable objects somewhat behave like functions.
An object that stores references to other objects are containers.
CPython: Include/cpython/object.h and Include/object.h.
A descriptor is an object that defines one of the dunders __get__, __set__ and __delete__.
A str representation of the object obj is returned by the built-in function str(obj).
The __repr__() method, if defined, returns a represenation of the object as a string.
The method inspect(obj) of the Rich package creates a nice report on any Python object.

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...', 1740536867, '3.135.222.112', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/objects/index(150): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78