How to create objects on the fly in python?

前端 未结 8 2232
野的像风
野的像风 2020-12-17 09:34

How do I create objects on the fly in Python? I often want to pass information to my Django templates which is formatted like this:

{\'test\': [a1, a2, b2],          


        
8条回答
  •  醉酒成梦
    2020-12-17 10:11

    use building function type: document

    >>> class X:
    ...     a = 1
    ...
    >>> X = type('X', (object,), dict(a=1))
    

    first and second X are identical

提交回复
热议问题