_fields Attribute Of The Namedtuple Class From The Python Collections Module

Attribute Name:

_fields

Overview:

  • A namedtuple instance enables creating new tuple types. Fields or elements of the tuples are given names.

  • The _fields attribute is a tuple of field names in a named tuple.

 

Example:

# Example Python program to print the field names of a namedtuple

 

import collections

 

def printErrorSchema(e):

    print(e._fields)

 

def printError(e):

    print(e.Error_Code);

    print(e.Error_Description);

 

 

# Create a custom error

customerror   =  collections.namedtuple("Error", "Error_Code, Error_Description");

 

# Create customerror instances

e1            = customerror("101", "Unresolved external symbol");

e2            = customerror("102", "Type mistmatch");

 

# Print the last error created and look at the error schema

printError(e2);

printErrorSchema(e2);

 

Output:

102

Type mistmatch

('Error_Code', 'Error_Description')

<class 'tuple'>


Copyright 2023 © pythontic.com