파이썬[Python]: collections - _fields 변수

collections 모듈 - _fields 변수(variable)


/// 설명

필드(field) 이름을 표현하는 튜플이며, 필드를 합쳐서 새로운 named 튜플을 생성하는데 유용합니다.

※ 형식
namedtuple._fields

reference
https://docs.python.org/3/library/collections.html#module-collections

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import collections
 
Test1 = collections.namedtuple('Test1', ['a''b''c'])
 
test_nt = Test1(a=1, b=2, c=3)
print(test_nt)  # Test1(a=1, b=2, c=3)
print(test_nt._fields)  # ('a', 'b', 'c')
 
Test2 = collections.namedtuple('Test2', ['d''e'])
 
test_nt = Test2(d=5, e=6)
print(test_nt)  # Test2(d=5, e=6)
print(test_nt._fields)  # ('d', 'e')
 
Test3 = collections.namedtuple('Test3', Test1._fields + Test2._fields)
test_nt = Test3(a=3, b=4, c=5, d=6, e=7)
print(test_nt)  # Test3(a=3, b=4, c=5, d=6, e=7)
print(test_nt._fields)  # ('a', 'b', 'c', 'd', 'e')
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수