views:

216

answers:

2

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
+4  A: 

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.

Steve Jessop
Indeed, perhaps you are correct. How else could I access that data?
Rosarch
get could do `self.CSIN = CSIN`, and any other attributes needed, before calling `body`. Handlers could then read `self.CSIN` if they need to, and not if they don't. Only do this if the CSIN really is a property of the whole request, though, not just a property of this *particular* call to body. If you might make more than one call to `body` on the same handler object, with different values of CSIN, then it's not a property of the handler, it really is a function parameter. Otherwise, you can stick whatever data on the handler that you need if it makes things work cleanly.
Steve Jessop
... another option is to have the BaseHandler call different methods according to what it actually needs. Sometimes you realise you've tried to cram too many special cases into a single abstraction, and really "body" doesn't serve the same purpose in all its incarnations. That's probably not the case here, if the only difference is whether or not a CSIN is needed to render the body, but if you let things get out of hand it might end up there.
Steve Jessop
+1  A: 

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.

Anatoly Fayngelerin
"the best kind of correct" - quite right, my "not necessarily" was hopelessly vague, and failed to point out that in this case it becomes necessary...
Steve Jessop
Good fix. I've removed the reference to your answer in mine.
Anatoly Fayngelerin