5.4. The SW-Design of namedtuplex¶
The basic Call Flow of Class Creation is adapted here to the special requirements of the creation of named tuples.
5.4.1. Creation of the Named Tuple¶
The class provided by the call of the factory function namedtupledefs.namedtuple, or alternatively by the tuplefactory collections.namedtuple, is extended by the abstract class NamedTupleXABC. The fabrication of the class is processed by the metaclass NamedTupleXABCMeta, where the provided named tuple class is set as a base class of the application interface NamedTupleXABC. The abstract class NamedTupleXABC provides a base class for the feature extension implemented by the class AppClass through inheritance.

Figure: Class Construction Layers for namedtuple ¶
5.4.1.1. Creation by Inheritance¶
The class interfaces namedtuplex.abc.NamedTupleXABC and namedtuplex.abc.ABC provide the creation of the named tuple by inheritance. The parameters are designed to be passed as atributes in this case. E.g. the attribute _fields of the namedtuple contains the symbolic names of the fields of the tuple. For the complete list refer to API prameters.
5.4.1.2. Creation by the Factory¶
The class factory namedtuplex.abc.namedtuplex provides a call interface with the dynamic creation of the class. The factory therefore supports more options, which are passed as call parameters to the function.
The field names are provided for the standard call of namedtupledefs.namedtuple as parameters, for example with fielddefaults:
namedtupledefs.namedtuple('ClsName', ('field0', 'field1',), fielddefaults=(value1,))
or
namedtupledefs.namedtuple('ClsName', ('field0', 'field1',), fielddefaults=(value0, value1,))
For the standard library of collections.namedtuple without the fielddefaults feature, e.g.:
collections.namedtuple('ClsName', ('field0', 'field1',))
5.4.2. Class Interface Parameters NamedTupleX¶
The namedtuplex package extends the standard interface. This includes multiple parameters provided as class variables for the class and the instance creation, see also [customclass]. For an example of the processing and inheretance of namedtuple see implementation by Jan Kaliszewski [JANKALI] [NAMEDTUPLEABC].
The provided class variables of derived classes are processed by the __new__ method of the metaclass NamedTupleXABCMeta.
5.4.2.1. _fields and _fielddefaults¶
The namedtuplex supports the parameters _fields and _fielddefaults.
The implementation is based on the namedtuple factory. When default values are required, these are delegated to the factory class, which in case of namedtupledefs.namedtuple generates the appropriate default values for the call interface of the created class. Thus the default based dynamic class creation is required by the __new__ method of the adaptation of the tuple fields at the creation time by the __new__ method of NamedTupleXABCMeta.
In case of the factory collections.namedtuple default values are currently not available.

Figure: Custom Class Construction NamedTupleX ¶
The following calls are processed interfaces in the given order. The index numbers figure refers to the marks at the previous figure.
interface
figure
processed class/object
called method
class param
processed
__new__
yes
__init__
NamedTupleXABC
NamedTupleXABCMeta.__init__
NamedTupleXABC
no
__new__
AppTupleClass
yes
__init__
AppTupleClass
NamedTupleXABCMeta.__init__
AppTupleClass
no
__new__
AppTupleClass
AppTupleClass.__new__
AppTupleClass
no
__new__
NamedTupleXABC
NamedTupleXABC.__new__
AppTupleClass
no
__init__
AppTupleClass (obj)
AppTupleClass.__init__
object<AppTupleClass>
no
__init__
NamedTupleXABC (obj)
NamedTupleXABC.__init__
object<AppTupleClass>
no
The application needs to define the marked “A” class member values _fields and optional the _fielddefaults. The value _fields defines the number of fields and their symbolic names for the named tuple, while the _fielddefaults member defines function interface style defaults [PYFUNC]. The _fields value is processed at the marked inteface “1.”, while the _fielddefaults value is processed at the marked interface “3.”.
The following example demonstrates the partial default values:
class AppTupleClass(NamedTupleXABC): _fields = ('a', 'b', 'c', 'd') _fielddefaults = (('c', 33), ('b', 22), ('d', 44)) # order is irrelevant myobj = AppTupleClass(11, 22) # allows partial calls print(myobj)
results in:
(11, 22, 33, 44)
The following example demonstrates the case default values only. In this case the default values replace the _fileds parameter completely:
class AppTupleClass(NamedTupleXABC): _fields = () # when empty _fielddefaults = (('a', 11), ('c', 33), ('b', 22), ('d', 44)) # _fielddefaults is used - allows empty calls # order is relevant myobj = AppTupleClass() # allows empty calls print(myobj)
results in:
(11, 33, 22, 44)
See also fields and _fields and fielddefaults and _fielddefaults.
5.4.2.2. _module¶
The module name is available beginning with Python-3.6. The value replaces the member variable __module__ of the created tuple.
See also module and _module.
5.4.2.3. _rename¶
The rename defines the optional renaming of invalid field names. This is transparently passed to the namedtuple class.
See also rename and _rename.
5.4.2.4. _tuplefactory¶
The tuplefactory defines the used factory function for the creation of the named tuple. This is by default namedtupledefs.namedtuple, and could be changed to a compatible alternativesuch as collections.namedtuple.
See also tuplefactory and _tuplefactory.
5.4.2.5. _verbose¶
The verbose is processed by the namedtuple factory and prints the dynamic created tuple class [namedtuple] and [namedtupledefs]. Thi parameter is for debugging only.
See also verbose and _verbose.
5.4.3. Parameters of namedtuple¶
The following parameters are passed to the collections.namedtuple [namedtuple].
See also usage of parameters.