Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Can subclasses overload inherited methods?

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
like image 873
Nick Heiner Avatar asked Mar 08 '10 00:03

Nick Heiner


1 Answers

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.

like image 115
Steve Jessop Avatar answered Oct 21 '22 22:10

Steve Jessop