PL/Python

[Python] struct(구조체) 사용하기

SweetDev 2021. 12. 27. 15:55

파이썬에는 원래 struct가 없다...

namedtuple이라는 라이브러리를 사용해서 구현할 수 있었다.

 

from collections import namedtuple

MyStruct = namedtuple("MyStruct", "a b c")
m = MyStruct("a", "b", "c")

====파이썬 3.7이상이면 dataclass를 사용할 수 있다.

 

파이썬의 decoration(자바의 annotation같은거) 를 쓰면 된다. 

 

from dataclasses import dataclass

@dataclass
class Product:
    weight:int = None
    price:int = None

apple = Product()
apple.weight = 10