The Noobs of Python: Ep.4.0 - What's your Function / Define your Function

Something that's been left untouched about functions is how to specify keyword arguments!

To specify a keyword argument, all you have to do is specify your keyword in the parentheses after the name of the function you're declaring, and then give it a default value to take if it's not otherwise specified when your function's called.

e.g. with these quotes, I was mildly upset there were no famous cat quotes! Like the infamous "Meow"! So, I added a cheat to get a meow whenever I want ;p

from random import choice

def Quotes(Classic_Quote,  cheat=False):
    if not cheat:
        return {"a":"Luke I am your Father !!!",
                "b":"When nine hundred years old you reach, look as good you will not",
                "c":"Truly wonderful, the mind of a child is.",
                "d":"That is why you fail.",
                "e":"A Jedi uses the Force for knowledge and defense, never for attack.",
                "f":"Adventure. Excitement. A Jedi craves not these things.",
                "g":"Judge me by my size, do you?",
                "h":"Fear is the path to the dark side…fear leads to anger…anger leads to hate…hate leads to suffering",
                "i":"Wars not make one great.",
                "j":"Luminous beings are we…not this crude matter.",
                "e":"Do. Or do not. There is no try.",
                "e":"Never tell me the odds!"}.get(Classic_Quote)
    else:
        return "MEOW!"
    
ran = choice('abcdefghijkl')

txt = Quotes(ran, cheat=True)
print(txt)

I can call this function without issues omitting the keyword argument as it's optional (it'll just take its default value), like this...

txt = Quotes(ran)
print(txt)

and I can also omit the keyword (this also works for multiple keyword arguments, assuming you know the order they're in at the function's declaration).

txt = Quotes(ran, True)
print(txt)
1 Like