Peter de Pradines
Peter de Pradines

Reputation: 1

Django function params

Where there are a series of parameters passed to a Django function they may have defaults. If a param is supplied when the function is called it is used. If not the default is used. Is there a way to access the first default while supplying subsequent param[s] in the function call?

 Example:

    def pong(fname = 'Pinkus', lname = 'Poke'):
    print(f'My name is {fname} {lname}')
    
    pong()                          # My name is Pinkus Poke
    pong('Frank')                   # My name is Frank Poke
    pong('Frank', 'Finklestein')    # My name is Frank Finklestein
    pong('', 'Bloggs')              # only gives empty string, no default!

Upvotes: 0

Views: 71

Answers (2)

Jimmy Pells
Jimmy Pells

Reputation: 714

You can pass the arguments as keyword arguments (kwargs) instead of positional arguments (args). This way you can specify which argument you want to set.

pong(lname='Bloggs')

Upvotes: 1

shivankgtm
shivankgtm

Reputation: 1242

def pong(fname = 'Pinkus', lname = 'Poke'):
    if not fname:
        fname = 'Pinkus'
    if not lname:
        lname = 'Poke

    print(f'My name is {fname} {lname}')

Upvotes: 0

Related Questions