[英]What is the most Pythonic way to organise constants?
I use marshmallow
and have a series of object serialization.我用
marshmallow
,有一系列的 object 序列化。 Because of technical reasons, the names of the field and class can be the same.由于技术原因,字段名称和 class 可以相同。
For example,例如,
class Sample(Schema):
SampleField = fields.Nested(SampleField)
class SampleField(Schema):
# Some other fields
When I need to reference SampleField
as a string in the code.当我需要在代码中将
SampleField
作为字符串引用时。 I create constant and name it SAMPLE_FIELD = SampleField
.我创建常量并将其命名为
SAMPLE_FIELD = SampleField
。 Often I need to have the same constant in both places in the definition class and where it is used as a field.通常我需要在定义 class 的两个地方以及它用作字段的地方都有相同的常量。
How to organize it the way, it will not become messy?怎样整理它的方式,才不会变得乱七八糟?
The marshmallow field does not have to be named after the field name in serialiazed data.棉花糖字段不必以序列化数据中的字段名称命名。
Would this solve your problem?这会解决你的问题吗?
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.