Python:子类可以重载继承的方法吗? [英] Python: Can subclasses overload inherited methods?

查看:122
本文介绍了Python:子类可以重载继承的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Google App Engine中制作购物车应用程序。我有许多类派生自一个基础处理程序:

$ p $ class BaseHandler(webapp.RequestHandler):
def get(自我,CSIN =无):
self.body(CSIN)

这是否意味着每个后代类的 body()方法需要具有相同的参数?这很麻烦。只有一个后代实际上使用该参数。那么当我添加新的参数时呢?我需要通过并改变每个类?

  class Detail(BaseHandler):
def body(self, CSIN):
$ b $ class MainPage(BaseHandler):
def body(self,CSIN = None):#@ UnusedVariable
$ b $ class Cart(BaseHandler):
def body(self,CSIN):#@ UnusedVariable


解决方案

<重写的方法原则上不必具有相同的参数,但它们必须具有相同的形式参数才能被调用。因此,由于任何处理程序都可以通过 get 调用 body ,所以它们必须相同。对此,重写的一点是调用者不知道对象的确切类别,因此如果他们不都具有相同的参数,则调用者通常不知道要传递什么。所以,覆盖不同的参数将是一个不寻常的小技巧,我认为。



如果你改变它的参数,那么你必须改变函数来匹配。这与继承无关,它是Python函数的工作方式。



如果您想要更多的灵活性,您可以使用关键字参数,这是传递一个字典作为参数:

$ $ p $ class Detail(BaseHandler):
def body(self,** kwargs):
print kwargs ['CSIN']

class MainPage(BaseHandler):
def body(self,** kwargs):#可以忽略kwargs

class Cart(BaseHandler):
def body(self,** kwargs):#可以忽略kwargs
$ b $ class BaseHandler(webapp.RequestHandler):
def get(self, CSIN =无):
self.body(CSIN = CSIN,some_new_arg = 3)

类SomeNewHandler(BaseHandler):
def body(self,** kwargs):
print kwargs ['some_new_arg']

我略微质疑这一点的智慧:如果你要添加很多新参数,并且大多数实现忽略大多数参数,那么马ybe body 并不是那些参数的函数。也许实际上这些参数是处理程序对象状态的一部分,你恰好是作为参数传递的。显然,这种差异是有些主观的 - 对于每个对象只调用一次的函数,在传递字典和使用 self 作为字典之间没有太大的实际区别。


I'm making a shopping cart app in Google App Engine. I have many classes that derive from a base handler:

class BaseHandler(webapp.RequestHandler):
    def get(self, CSIN=None):
        self.body(CSIN)

Does this mean that the body() method of every descendant class needs to have the same argument? This is cumbersome. Only one descendant actually uses that argument. And what about when I add new args? Do I need to go through and change every class?

class Detail(BaseHandler):
    def body(self, CSIN):

class MainPage(BaseHandler):
    def body(self, CSIN=None): #@UnusedVariable

class Cart(BaseHandler):
    def body(self, CSIN): #@UnusedVariable

解决方案

Overridden methods don't have to have the same parameters as each other in principle, but they do have to have the same formal parameters they're called with. So since any handler can have body called on it by get, yes they have to be the same. For that matter, kind of the point of overriding is that the caller doesn't know the exact class of the object, and hence if they don't all have the same parameters, normally the caller wouldn't know what to pass. So overrides with different parameters would be an unusual bit of trickery, I think.

If you change the args it's called with then yes, you have to change the functions to match. This has nothing to do with inheritance, it's how Python functions work.

If you want a bit more flexibility, you could use keyword arguments, which are a fancy way of passing a dictionary as an argument:

class Detail(BaseHandler):
    def body(self, **kwargs):
        print kwargs['CSIN']

class MainPage(BaseHandler):
    def body(self, **kwargs): # can ignore kwargs

class Cart(BaseHandler):
    def body(self, **kwargs): # can ignore kwargs

class BaseHandler(webapp.RequestHandler):
    def get(self, CSIN=None):
        self.body(CSIN = CSIN, some_new_arg = 3)

class SomeNewHandler(BaseHandler):
    def body(self, **kwargs):
        print kwargs['some_new_arg']

I do slightly question the wisdom of this, though: if you're going to be adding new parameters a lot, and most implementations ignore most parameters, then maybe body isn't really a function of those arguments. Maybe actually the arguments are part of the state of the handler object, that you just happen to be passing as parameters. Obviously the difference is somewhat subjective - for functions only called once per object there's not a whole lot of practical difference between passing a dictionary, and using self as the dictionary.

这篇关于Python:子类可以重载继承的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆