collections in namedtuple usage

We know tuple can represent the same collection, for example, two-dimensional coordinates of a point can be expressed as:

p = (1, 2)

However, see (1, 2), it is difficult to see this tuple is used to represent a coordinate. At this time, namedtuple came in handy.

usage:

namedtuple ( 'name', [Properties List])

Examples of the use of a coordinate namedtuple represented as follows:

from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)

print(p.x,p.y)

Output: `2 '

Another example of Densenet:

from collections import namedtuple

DensenetParams = namedtuple('DensenetParameters', ['num_classes',
                                         'first_output_features',
                                         'layers_per_block',
                                         'growth_rate',
                                         'bc_mode',
                                         'is_training',
                                         'dropout_keep_prob'
                                         ])

default_params = DensenetParams(
        num_classes = 10,
        first_output_features = 24,
        layers_per_block = 12,
        growth_rate = 12,
        bc_mode = False,
        is_training = True,
        dropout_keep_prob = 0.8,
        )

print(default_params.num_classes)

The output is:10

Guess you like

Origin www.cnblogs.com/Terrypython/p/11293744.html