mflorczak
mflorczak

Reputation: 363

Keyword argument with defaults first and second param

I want to create function with two default params and other that will be *args and **kwargs

I have function with params and keywords. If in function I set first two values everything works good but I want to that this params will be default. How can I do this?

def the_credits(length_of_line=80, empty_space_between_lines=0, *args, **kwargs):
    for arg in args:
        for a in str(arg).center(length_of_line // 2):
            print(a, end='')
            if a != " ":
                time.sleep(0.4)
        print('\n' * empty_space_between_lines)
    for kwarg in kwargs:
        for k in (kwarg + ' : ' + kwargs[kwarg]).center(length_of_line // 2):
            print(k, end='')
            if k != " ":
                time.sleep(0.4)
        print('\n' * empty_space_between_lines)
    input()


the_credits("Cudny Program Pokazowy",
            "Zemsta zlego markiza Cul-de-Sac",
            Scenariusz="Autor",
            Scenografia="Autor",
            Kamerowanie="Brak",
            Kostiumy="Natura",
            Podziekowana="Pyton")

This example works

the_credits(80, 0, "Cudny Program Pokazowy",
            "Zemsta zlego markiza Cul-de-Sac",
            Scenariusz="Autor",
            Scenografia="Autor",
            Kamerowanie="Brak",
            Kostiumy="Natura",
            Podziekowana="Pyton")

Upvotes: 0

Views: 43

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49803

You could just use kwargs & args and specify these problematic parameters by name; if length_of_line or empty_space_between_lines isn't in kwargs, you know to use the default values.

Upvotes: 2

Related Questions