Namedtuple
Namedtuple is a built-in type of system that inherits from a tuple in the collections module.
How to use:
collections.namedtuple (typename,field_names[, verbose=false][, Rename=false])
namedtuple Returns a subclass of tuples named with TypeName .
This new subclass is used to create a tuple-like object, but it can be accessed using attributes, and can be indexed and iterated.
Here the field_names can be written['x','y'] , [ ' x y '] , or ['x, y']
If rename=true , illegal fieldname will automatically be changed to the location name.
Such as
['abc'def'ghi',' ABC']
will be converted into
['abc'def'ghi',' ABC ']
, because the keyword def makes the property name illegal, ABC is because of the repetition.
If verbose=true , a class is created.
Here's an example:
Point = Namedtuple (' Point',['x','y'])>>> p = Point (11,y=22)>>> P[0]+p[1]#indexes can be used as normal tuples33>>> x, y = P#can unpack>>>x, Y (11, 22)>>> p.x+p.y#You can use attributes to access elements33>>>ppoint (x=11, y=22)
Python common built-in modules