在普通的 python 类中,我可以定义类属性,例如
class Example:
x = 3
def __init__(self):
pass
Run Code Online (Sandbox Code Playgroud)
如果我接着做Example.xor Example().x,我就得到了3。
当我继承 pydantic's 时BaseModel,我不知道如何定义类属性,因为定义它们的通常方法被 BaseModel 覆盖。
例如:
class Example(BaseModel):
x = 3
print(Example.x)
--> type object 'Example' has no attribute 'x'
Run Code Online (Sandbox Code Playgroud)
我希望能够定义类级别属性。正确的方法/语法是什么?
考虑一下我写的这个宏:
{% macro concatenate_columns() %}
{% for column in [col_1, col_2, col_3] %}
"{{column}}" ||
{% endfor %}
''
{% endmacro %}
Run Code Online (Sandbox Code Playgroud)
该宏的要点是获取一个列数组并将它们连接在一起。但是,在之后{% endfor %}我必须包含 a''以防止宏将最终字符返回为||。
这是我在 for 循环中遇到的一个常见问题。这在语法上并不难处理,但我遇到过一种情况,我UNION在循环之间使用了 s,并且最后添加的位变得非常复杂。
是否有一种干净的方法来编写 for 循环,其中最终循环“束缚”输出,并且不继续附加?