attr库

代码展示:

​@attr.s(slots=True, frozen=True)#https://www.jianshu.com/p/2140b519028d
class Context:
    """The context that triggered something."""

    user_id = attr.ib(#attr.ib则是快速对属性进行定义的方法
        type=str,
        default=None,
    )
    parent_id = attr.ib(
        type=Optional[str],
        default=None
    )
    id = attr.ib(
        type=str,
        default=attr.Factory(lambda: uuid.uuid4().hex),
    )

    def as_dict(self) -> dict:
        """Return a dictionary representation of the context."""
        return {
            'id': self.id,
            'parent_id': self.parent_id,
            'user_id': self.user_id,
        }​

参考资料1

 

 

你可能感兴趣的:(Python)