namedtuple 을 사용하면 class보다 간편하게 사용가능합니다.
TUPLE 말뜻대로 배열에 이름표를 붙여서 사용하는데
c의 structure 같다는 느낌입니다.
from collections import namedtuple class Fruit(object): def __init__(self, name, colour, shape): self.name = name self.colour = colour self.shape = shape variables1 = {} variables2 = {} Fruitlist = [ ['Apple', 'red', 'circle'], ['Banana', 'yellow', 'abnormal'], ['Pear', 'green', 'abnormal'] ] Fruits = namedtuple("Fruit",
["name", "colour", "shape"]) Fruitlist.append(["감","노랑","동그라미"]) for args in Fruitlist: fruit1 = Fruit(*args) fruit2 = Fruits(*args) variables1[fruit1.name] = fruit1 variables2[fruit2.name] = fruit2 print("Apple shape: " + variables1["감"].shape) print("Apple shape: " + variables2["감"].shape)
http://stackoverflow.com/questions/11118486/python-list-as-variable-name
댓글 없음:
댓글 쓰기