Python common built-in modules

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.