Class factory to produce simple struct-like classes?

后端 未结 7 1439
盖世英雄少女心
盖世英雄少女心 2020-12-08 05:47

While investigating Ruby I came across this to create a simple Struct-like class:

Person = Struct.new(:forname, :surname)
person1 = Person.new(\'John\', \'Do         


        
7条回答
  •  孤城傲影
    2020-12-08 06:19

    There is namedtuple

    >>> from collections import namedtuple
    >>> Person = namedtuple("Person", ("forename", "surname"))
    >>> john = Person("John", "Doe")
    >>> john.forename 
    'John'
    >>> john.surname 
    'Doe'
    

提交回复
热议问题