Home > OS >  Django function params
Django function params

Time:02-21

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!

CodePudding user response:

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')

CodePudding user response:

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

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