Is it possible to create anonymous objects in Python?

前端 未结 11 602
余生分开走
余生分开走 2020-12-14 13:58

I\'m debugging some Python that takes, as input, a list of objects, each with some attributes.

I\'d like to hard-code some test values -- let\'s say, a list of four

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 14:38

    Have a look at this:

    
    class MiniMock(object):
        def __new__(cls, **attrs):
            result = object.__new__(cls)
            result.__dict__ = attrs
            return result
    
    def print_foo(x):
        print x.foo
    
    print_foo(MiniMock(foo=3))
    

提交回复
热议问题