如何继承自己的class in output class of Google API in python?
How to inherit own class in output class of Google API in python?
我为错误消息创建了自己的 Common class
# for returning error messages
class ErrorMessage(messages.Message):
errorCode = messages.StringField(1)
developerMessage = messages.StringField(2)
userMessage = messages.StringField(3)
和端点
的输出class
# output class for GetFriendlyFieldNames
class GetFriendlyFieldNamesOutput(messages.Message):
friendlyNames = messages.MessageField(GetFriendlyFieldNamesStore, 1,repeated=True)
我试图继承多个 class 到我的输出 class 并将上面的 class 更改为
# output class for GetFriendlyFieldNames
class GetFriendlyFieldNamesOutput(messages.Message,ErrorMessage):
friendlyNames = messages.MessageField(GetFriendlyFieldNamesStore, 1,repeated=True)
但是现在这在日志中显示错误
"Message types may only inherit from Message"
这是 Google Proto RPC 库的限制。在 its source code 中,您会发现这是不可能的。
它只允许messages.Message的一个直接子class:
if bases != (object,):
# Can only define one level of sub-classes below Message.
if bases != (Message,):
raise MessageDefinitionError(
'Message types may only inherit from Message')
我为错误消息创建了自己的 Common class
# for returning error messages
class ErrorMessage(messages.Message):
errorCode = messages.StringField(1)
developerMessage = messages.StringField(2)
userMessage = messages.StringField(3)
和端点
的输出class# output class for GetFriendlyFieldNames
class GetFriendlyFieldNamesOutput(messages.Message):
friendlyNames = messages.MessageField(GetFriendlyFieldNamesStore, 1,repeated=True)
我试图继承多个 class 到我的输出 class 并将上面的 class 更改为
# output class for GetFriendlyFieldNames
class GetFriendlyFieldNamesOutput(messages.Message,ErrorMessage):
friendlyNames = messages.MessageField(GetFriendlyFieldNamesStore, 1,repeated=True)
但是现在这在日志中显示错误
"Message types may only inherit from Message"
这是 Google Proto RPC 库的限制。在 its source code 中,您会发现这是不可能的。
它只允许messages.Message的一个直接子class:
if bases != (object,):
# Can only define one level of sub-classes below Message.
if bases != (Message,):
raise MessageDefinitionError(
'Message types may only inherit from Message')