Python: Can subclasses overload inherited methods?

2019-01-28 04:20发布

问题:

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

回答1:

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.



回答2:

Python matches methods for overloading based on name only. Which means that

class Base:
  def method(self, param2):
     print "cheeses"

class NotBase(Base):
  def method(self):
     print "dill"

obj = NotBase();
obj.method() 

will output dill( while obj.method("stuff") will fail ).

However, in your case this isn't the desirable behavior. If you overload the body method with fewer parameters than required by the invocation in the base get method, invoking the get method on such classes will result in an error.