![](/img/trans.png)
[英]What is the most elegant / pythonic way of introducing large integer constants?
[英]What is the most Pythonic way to organise constants?
我用marshmallow
,有一系列的 object 序列化。 由于技术原因,字段名称和 class 可以相同。
例如,
class Sample(Schema):
SampleField = fields.Nested(SampleField)
class SampleField(Schema):
# Some other fields
当我需要在代码中将SampleField
作为字符串引用时。 我创建常量并将其命名为SAMPLE_FIELD = SampleField
。 通常我需要在定义 class 的两个地方以及它用作字段的地方都有相同的常量。
怎样整理它的方式,才不会变得乱七八糟?
棉花糖字段不必以序列化数据中的字段名称命名。
这会解决你的问题吗?
class SampleSchema(Schema):
sample_field = fields.Nested(SampleFieldSchema, data_key='SampleField')
class SampleFieldSchema(Schema):
# Some other fields
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.