2014-01-15 2 views
0

Я хочу добавить необязательный именованный аргумент в конструктор подкласса. Если не указано, аргументы должны быть такими же, как для базового суперкласса. Например.Именованные аргументы при подклассов в Python

class Foo(object): 
     def __init__(self, *args, **kwargs): 
       print 'args', args 
       print 'kwargs', kwargs 


class Bar(Foo): 
     # Here I want hello to be named-only, so that passing `hello` would be 
     # optional, and all arguments would otherwise be passed to `Foo`. 
     # However, such a syntax is incorrect 
     #def __init__(self, *args, hello=False, **kwargs): 
     #  Foo.__init__(self, *args, **kwargs) 

     # I can do this instead. But this always requires passing `hello` as 
     # the first argument 
     def __init__(self, hello=False, *args, **kwargs): 
       Foo.__init__(self, *args, **kwargs) 


# Prints `args (2, 3)` and `kwargs {'for_foo': 4}`, but I want args to be 
# `(1, 2, 3)` instead and 'hello` to be `False` instead of `1` 
f = Bar(1, 2, 3, for_foo=4) 

# This wouldn't work at all, since `hello` is passed twice. I want args 
# to be printed as `(1, 2, 3)` again, and `hello` to be `True` and retained 
# by `Bar.__init__` 
f = Bar(1, 2, 3, hello=True) 

Есть ли шаблон для таких случаев? Каков правильный способ сделать это?

ответ

1
class Bar(Foo): 
    def __init__(self, *args, **kwargs): 
     try: 
      hello = kwargs.pop('hello') 
     except KeyError: 
      hello = False 
     Foo.__init__(self, *args, **kwargs) 
+0

... или один может использовать аналогичное решение от связанного вопроса, который не использует обработку исключений (и, следовательно, может быть быстрее) – dragonroot

0

Попробуйте получить «привет» от kwargs. Если есть, удалите его и передать меньшие kwargs в конструктор суперкласса, иначе просто вызвать конструктор суперкласса:

 
#in Bar: 
def __init__(self, *args, **kwargs): 
    hello = None 
    if "hello" in kwargs: 
     hello = kwargs["hello"] 
     del kwargs["hello"] 
    Foo.__init__(self, *args, **kwargs) 
    if not hello is None: 
     do_something_with_hello(hello) 
Смежные вопросы