Search notes:

Python standard library: collections.namedtuple

collections.namedtuple allows to create objects with a given set of field names (attributes), somewhat similar to a struct in C.
from collections import namedtuple

NTcls = namedtuple(
          'NT'                     , # class name
          ['val1', 'val2', 'val3']   # field names
        )

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

print([ d for d in dir(NTcls) if not d.startswith('__') ]) # ['_asdict', '_field_defaults', '_fields', '_make', '_replace', 'count', 'index', 'val1', 'val2', 'val3']

print(NTcls._fields)                                       # ('val1', 'val2', 'val3')

ntObj1 = NTcls('foo', 'bar', 'baz')
print(ntObj1)                                              # NT(val1='foo', val2='bar', val3='baz')

ntObj2 = NTcls( val2='world',val1='hello', val3='!' )
print(ntObj2)                                              # NT(val1='hello', val2='world', val3='!')

ntObj3 = NTcls._make( ['x', 'y', 'z'] )
print(ntObj3)                                              # NT(val1='x', val2='y', val3='z')

ntDict = ntObj3._asdict()
print(ntDict['val2'])                                      # y

See also

The tuple type
typing.NamedTuple is the typed version of collection.namedtuple.

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