Skip to content Skip to sidebar Skip to footer

Default Argument Value From Overridden Method (preventing Boilerplate)

I have a base class Base and two sub classes Foo and Bar. In Base I define a function with an optional argument. This argument is either given or fetched at runtime (not at definit

Solution 1:

How about using a decorator? This is the sort of thing they are meant for. Something like:

defget_at_runtime(func):
    defwrapped(*args, **kwargs):
        if kwargs.get('name', None) isNone:
            kwargs['name'] = get_name_at_runtime()
        func(*args, **kwargs)

and wrap your methods:

@get_at_runtimedefgreet(self, name=None):
    print('Hello {name} this is Foo.'.format(name=name))

Solution 2:

Hi You can use metaclasses

classDefaultNameType(type):
    def__new__(cls, name, bases, attrs):
        if'greet'in attrs:
            attrs['_greet'] = attrs.pop('greet')
            defgreet(self, name=None):
                name = name or self.get_name_at_runtime()
                return self._greet(name)
            attrs['greet'] = greet
        print attrs
        returnsuper(DefaultNameType, cls).__new__(cls, name, bases, attrs)

classBase(object):
    __metaclass__ = DefaultNameType

    defget_name_at_runtime(self):
        return'foo'defgreet(self, name):
        passclassFoo(Base):
    defgreet(self, name):
        print('Hello {name} this is Foo.'.format(name=name))

classBar(Base):
    defgreet(self, name):
        print('Hello {name} this is Bar.'.format(name=name))    

    defget_name_at_runtime(self):
        return'foo1'

In this case any class derived from Base will be modified so that method greet will be renamed to _greet and greet method will be created which will run _greet

Post a Comment for "Default Argument Value From Overridden Method (preventing Boilerplate)"