มีอะไรเหมือน Struct.new ของ Ruby บน Python หรือเปล่า?

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

ปกติเวลาจะสร้าง class ที่เอาไว้เก็บ data อย่างเดียวผมมักจะใช้

Word = Struct.new("Word", :surface, :pos)
w1 = Word.new("house", "noun")

อะไรประมาณนี้เอาใน Ruby อะนะ เวลาใช้ Python ก็ใช้ dict(surface="house", pos="noun") ก็ยังรู้สึก dict ว่ามันทั่วไปเกินไปนิดๆ อยู่ดีกลับมาอ่านทีหลังแล้วงง

ไม่รู้ว่าพอมีอะไรที่ใช้เหมือน Struct ใน Python เปล่า?

sugree's picture

เหมือนจะไม่มีนะ ถ้าเบื่อๆ ก็ใช้ class ก็ได้นี่

class MyStruct:
    def __init__(self,**kw):
        setl.__dict__.update(kw)
 
n = MyStruct(surface="house", pos="noun")
veer's picture

ขอบคุณครับ

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น