[英]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.