简体   繁体   English

Python扩展父类的方法

[英]Python extend method of parent class

I'm writing a Google Appengine app in Python. 我正在用Python编写Google Appengine应用程序。 The app does receive and send data through google cloud endpoint messages. 该应用程序确实通过谷歌云端点消息接收和发送数据。 Below you'll find an example of the database model definition. 您将在下面找到数据库模型定义的示例。 Object contains all the objects, ObjectBoolean just the boolean ones and ObjectInteger just the integer ones. Object包含所有对象,ObjectBoolean只是布尔值,ObjectInteger只是整数对象。 All the classes do have got two methods defined: message_to_model() and model_to_message(). 所有类都定义了两个方法:message_to_model()和model_to_message()。

How can I re-use the methods in the sub-classes? 如何重用子类中的方法? I do want to get rid of repeated code like: 我想摆脱重复的代码,如:

        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified

Those fields are part of the Object-class. 这些字段是Object-class的一部分。 But in my code the fields are also listed in the sub-classes, which is what I do want to avoid. 但在我的代码中,字段也列在子类中,这是我想要避免的。 How can I achieve this? 我怎样才能做到这一点? I did already check the super() function, but I think this is not suitable in my case because I do not want to execute the method I do want to extend it. 我已经检查了super()函数,但我认为这不适合我的情况,因为我不想执行我想扩展它的方法。

Cheers 干杯

-Luca. -Luca。

from google.appengine.ext.ndb import polymodel
from google.appengine.ext import ndb
from endpoint_messages.object import *


class Object(polymodel.PolyModel):
    name = ndb.StringProperty(required=True, verbose_name="Name")
    description = ndb.TextProperty(verbose_name="Description")
    created = ndb.DateTimeProperty(auto_now_add=True, verbose_name="Created")
    modified = ndb.DateTimeProperty(auto_now=True, verbose_name="Modified")

    def model_to_message(self):
        return ObjectResponseMessage(class_name=self._class_name(),
                                     id=self.key.integer_id(),
                                     name=self.name,
                                     description=self.description,
                                     created=self.created,
                                     modified=self.modified)

    def message_to_model(self, request):
        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified


class ObjectBoolean(Object):
    current = ndb.BooleanProperty(verbose_name="Current")
    target = ndb.BooleanProperty(verbose_name="Target")

    def model_to_message(self):
        return ObjectResponseMessage(class_name=self._class_name(),
                                     id=self.key.integer_id(),
                                     name=self.name,
                                     description=self.description,
                                     created=self.created,
                                     modified=self.modified,
                                     boolean=ObjectBooleanMessage(current=self.current,
                                                                  target=self.target))

    def message_to_model(self, request):
        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified
        if request.boolean:
            self.current = request.boolean.success
            self.target = request.boolean.target


class ObjectInteger(Object):
    current = ndb.IntegerProperty(verbose_name="Current")
    target = ndb.IntegerProperty(verbose_name="Target")

    def model_to_message(self):
        return ObjectResponseMessage(class_name=self._class_name(),
                                     id=self.key.integer_id(),
                                     name=self.name,
                                     description=self.description,
                                     created=self.created,
                                     modified=self.modified,
                                     float=ObjectIntegerMessage(current=self.current,
                                                                target=self.target))

    def message_to_model(self, request):
        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified
        if request.integer:
            self.current = request.integer.current
            self.target = request.integer.target

super() can be used for this. super()可以用于此。 You could use something like this in ObjectBoolean : 你可以在ObjectBoolean使用这样的东西:

def message_to_model(self, request):
    super(ObjectBoolean, self).message_to_model(request)

    if request.boolean:
        self.current = request.boolean.success
        self.target = request.boolean.target

For this to work, Object must be a new style class, which it is if polymodel.PolyModel is a new style class. 为此, Object必须是一个新的样式类,如果polymodel.PolyModel是一个新的样式类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM