Class factory to produce simple struct-like classes?

后端 未结 7 1435
盖世英雄少女心
盖世英雄少女心 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:16

    The Python package esu brings a struct that can provide almost the same functionality:

    from esu import Struct
    
    Customer = Struct(
            'Customer',
            'name', 'address',
            methods={
                'greeting': lambda self: "Hello {}".format(self.__dict__['name'])
            })
    
    dave = Customer()
    dave.name = 'Dave'
    dave.greeting() # => Hello Dave
    

    from https://torokmark.github.io/post/python-struct/

提交回复
热议问题