The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 34 Python's Built-in Data Types - Common Composite Data Types in Python: Tuples and Named Tuples
Tuples are immutable sequence types that can contain elements of different types. Named tuples are subclasses of tuples, which allow you to assign names to the positions in a tuple, thus making the code more readable. This article mainly introduces the usage and application scenarios of these two types of tuples.
This document mainly introduces how to conduct Object Oriented programming with Python, and it requires readers to have a basic understanding of Python syntax and single-chip microcomputer development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications. It takes the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples, and uses the Sourcetrail code software to visually read the code to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows:
https://github.com/leezisheng/Python-OOP-Demo
Body Text
Tuple
A tuple is an object that can store a certain number of other objects in a defined order. They are immutable, which means that we cannot add, remove, or replace the objects within them during runtime. The biggest advantage of the immutability of tuple types is that they can be used as keys for dictionary types, as well as in other contexts where an object is required to have a hash value.
Tuples are used to store data and cannot store behaviors. If you need a certain behavior to manipulate a tuple, you must pass the tuple to a function (or a method of another object) to perform this operation.
Generally speaking, tuples are intended to store distinct values; for instance, you would not typically put the IDs of three sensors into a single tuple, but rather group a sensor's ID, its current reading, and its maximum and minimum recorded readings into one tuple. The core purpose of a tuple is to aggregate disparate pieces of data into a single container, which makes it the simplest tool for replacing "data-only objects" that carry no associated methods.
We can create a tuple using comma-separated values; parentheses are typically used for better readability and to distinguish the tuple from other statements, though they are not mandatory.
# The following two assignment syntaxes are equivalent (record a sensor's ID, current data value, maximum value and minimum value):
sensor_tuple = ("16",32,62,2)
print(sensor_tuple, type(sensor_tuple))
sensor_tuple = "16",32,62,2
print(sensor_tuple, type(sensor_tuple))
If tuples are used inside other constructs such as function calls, list comprehensions, or generators, parentheses are required. Otherwise, the interpreter has no way to tell whether it is a tuple or the next function argument. Meanwhile, tuple unpacking occurs when receiving the return value of a function: when a function returns data, it often combines variables together for simple storage and transfer, and when receiving the return value of the function, these variables can be unpacked into multiple separate variables.
Namedtuple
Since accessing tuple elements must be done via indices, it is often confusing to figure out the specific meaning corresponding to each subscript when a tuple contains too many elements. Meanwhile, when writing a required function for another developer, that developer is highly likely to encounter errors due to incorrect order or incorrect number of the returned values they receive.
If you neither need to add behaviors to an object nor know in advance what data to store, you can use a named tuple. A named tuple is a tuple with attributes, and it is an excellent way to combine read-only data. Compared with a regular tuple, constructing a named tuple requires slightly more work. First, you need to import namedtuple, as it is not in the default namespace. Then define a named tuple by specifying its name and attributes, which will return a class-like object that you can instantiate with the required values.
The constructor function of a named tuple accepts two parameters: typename and field_names. typename specifies the name of the tuple, while field_names specifies the names of each element in the tuple, i. e., the attribute names (a string of attributes separated by spaces).
from collections import namedtuple
Sensor = namedtuple("Sensor","ID CURRENTVALUE MAXVALUE MINVALUE")
sensor_tuple = Sensor(ID = "16",CURRENTVALUE = 32,MAXVALUE = 62,MINVALUE = 2)
# The attributes of a tuple can be accessed like the attributes of an object
print(sensor_tuple.CURRENTVALUE)
print(sensor_tuple,type(sensor_tuple),type(Sensor))
Named tuples are well-suited for representing "data-only" scenarios, but they are not ideal for every use case. Like tuples and strings, named tuples are immutable, meaning that once a value is assigned to an attribute, it cannot be modified. If you need to modify stored data, a dictionary type may be a more appropriate choice.