-
>>> from collections import namedtuple >>> >>> C1 <class '__main__.C1'> >>> c1 = C1('aa', 'bb', 'cc') >>> c1 C1(mA='aa', mB='bb', mC='cc') >>> c1.mB 'bb' >>> >>> C1([11,22,33]) TypeError: __new__() missing 2 required positional arguments: 'mB' and 'mC' >>> C1(*[11,22,33]) C1(mA=11, mB=22, mC=33) >>> C1._make([11,22,33]) C1(mA=11, mB=22, mC=33) >>>
응용 Db, Csv
Employee = namedtuple('Employee', 'name, age') import csv for emp in map(Employee._make, csv.reader(open("employeefile.csv", "rb"))): print emp.name, emp.age import sqlite3 conn = sqlite3.connect('/companydata') cursor = conn.cursor() cursor.execute('SELECT name, age, FROM employeestb') for emp in map(Employee._make, cursor.fetchall()): print emp.name, emp.age
'Python' 카테고리의 다른 글
Python - OpenCV (0) 2020.09.09 Python - "python-for-android" apk (0) 2020.09.09 Python - exe 파일 만들기 (0) 2020.09.09 Python - itertools (0) 2020.09.08 Python - Counter (0) 2020.09.08 Python - deque - temp (0) 2020.09.08 Python - Time Complexity (0) 2020.09.07 Python - reversed 클래스 함수 (0) 2020.09.07