Python: can subclasses overload inherited methods?

I am making a shopping cart application in Google App Engine. I have many classes that come from the base handler:

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

Does this mean that the method body()for each class of children must have the same argument? This is cumbersome. Only one descendant actually uses this argument. But what when I add new arguments? Do I need to go through and change each 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
+3
source share
2 answers

, , , . body, get, , . , , , , , , . , .

, , , . , Python.

, , :

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']

: , , , , body . , , . , - , , self .

+6

Python . ,

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

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

obj = NotBase();
obj.method() 

( obj.method( "stuff" ) ).

. body , get, get .

+2

Source: https://habr.com/ru/post/1735846/


All Articles