“Python Substitua Herited Method Class Model Construtor” Respostas de código

Python Substitua Herited Method Class Model Construtor

class Bar(object):
   def __init__(self, arg1=None, arg2=None, argN=None):
       print arg1, arg2, argN

class Foo(Bar):
    def __init__(self, my_new_arg=None, **kwds):
       super(Foo, self).__init__(**kwds)
       self.new_arg = my_new_arg
       print my_new_arg

f = Foo(my_new_arg='x', arg2='y')
DreamCoder

Python Substitua Herited Method Class Model Construtor

class Parent(object):
    def __init__(self, a, b):
        print 'a', a
        print 'b', b

class Child(Parent):
    def __init__(self, c, d, *args, **kwargs):
        print 'c', c
        print 'd', d
        super(Child, self).__init__(*args, **kwargs)

test = Child(1,2,3,4)
DreamCoder

Método herdado de substituição de python

import datetime

class Logger(object):
    def log(self, message):
        print message

class TimestampLogger(Logger):
    def log(self, message):
        message = "{ts} {msg}".format(ts=datetime.datetime.now().isoformat(),
                                      msg=message)
        super(TimestampLogger, self).log(message)
DreamCoder

Respostas semelhantes a “Python Substitua Herited Method Class Model Construtor”

Perguntas semelhantes a “Python Substitua Herited Method Class Model Construtor”

Procure respostas de código populares por idioma

Procurar outros idiomas de código