Python 中 import attr 是什么意思?

Ayn*_*ess 4 python

在Python代码中,我发现了这一行。这是什么意思?

import attr
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

import collections

import attr
import tensorflow as tf
Run Code Online (Sandbox Code Playgroud)

小智 6

attrs\xc2\xa0 是一个 Python 包,它为您提供了一个类装饰器以及一种以声明方式定义该类的属性的方法:。下面给出一个例子:-

\n
import attr \n@attr.s\nclass SomeClass(object):\n  a_number = attr.ib(default=42)    \n  list_of_numbers = attr.ib(factory=list) \n  def hard_math(self, another_number):   \n     return self.a_number \n     +sum(self.list_of_numbers) * \n     another_number \n
Run Code Online (Sandbox Code Playgroud)\n

有关详细说明,请访问https://pypi.org/project/attrs/

\n