Python: Can subclasses overload inherited methods?

前端 未结 2 483
遥遥无期
遥遥无期 2020-12-20 22:34

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          


        
2条回答
  •  無奈伤痛
    2020-12-20 23:15

    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.

提交回复
热议问题