Shrayas (@shrayasr)
Note: Won't spend too much time on the body of the functions unless required
def
def foobar():
print "Foobar"
foobar()
Foobar
def say_hello(name, language):
language_responses = {
"ENGLISH": "Hello, ",
"JAPANESE": "Konichiwa, ",
}
# Clean the input
language = language.strip().upper()
hello_str = language_responses.get(language)
if hello_str:
return hello_str + name
else:
return "Language not supported"
Arguments passed one after another
say_hello("Chennaipy", "English")
'Hello, Chennaipy'
say_hello("English", "Chennaipy")
'Language not supported'
Which of course means, the order in which they are passed, matters
Arguments passed into the function with a keyword
say_hello(name="Chennaipy", language="Japanese")
'Konichiwa, Chennaipy'
say_hello(language="Japanese", name="Chennaipy")
'Konichiwa, Chennaipy'
Which of course means, the order in which they are passed, doesn't matter
Positional
def square(*nums):
print type(nums)
print nums
square(2,3,4,6,7,8,5,4,3,2)
<type 'tuple'> (2, 3, 4, 6, 7, 8, 5, 4, 3, 2)
Keyword
def make_coffee(**config):
print type(config)
print config
make_coffee(decaf=False, milk=True, extra_sugar=True)
<type 'dict'> {'decaf': False, 'extra_sugar': True, 'milk': True}
Because, Python yo!
def illustrate_pos_kw_args(first_arg, *args, **kwargs):
print first_arg
print args
print kwargs
illustrate_pos_kw_args(1,2,3,a=1, b=2)
1 (2, 3) {'a': 1, 'b': 2}
def hello_world(name=None):
if name:
return "Hello, " + name
else:
return "Hello, world"
print hello_world()
print hello_world("Chennaipy")
Hello, world Hello, Chennaipy
def make_dosa(masala=False, red_chutney=False, extra_roast=False):
def _evaluate_dosa_configuration(masala, red_chutney, extra_roast):
print masala
print red_chutney
print extra_roast
return True
def _make_dosa(masala, red_chutney, extra_roast):
return " ______\n(______)"
if _evaluate_dosa_configuration(masala, red_chutney, extra_roast):
return _make_dosa(masala, red_chutney, extra_roast)
print make_dosa(masala=True)
True False False ______ (______)
def fn1(arg1, arg2, arg3):
def fn2():
print "from fn2", arg1, arg2, arg3
fn2()
fn1(1,2,3)
from fn2 1 2 3
def make_dosa(masala=False, red_chutney=False, extra_roast=False):
def _evaluate_dosa_configuration():
print masala
print red_chutney
print extra_roast
return True
def _make_dosa():
return " ______\n(______)"
if _evaluate_dosa_configuration():
return _make_dosa()
print make_dosa(masala=True)
True False False ______ (______)
def make_dosa(masala=False, red_chutney=False, extra_roast=False, \
method_of_preparation=None):
if not method_of_preparation:
raise Exception("Don't know how to make dosa")
def _evaluate_dosa_configuration():
return True
def _make_dosa():
return method_of_preparation()
if _evaluate_dosa_configuration():
return _make_dosa()
def chennai_dosa():
return " ___________\n(__CHENNAI__)"
def bangalore_dosa():
return " ___________\n(_BANGALORE_)"
print make_dosa(masala=True, method_of_preparation=chennai_dosa)
print make_dosa(masala=True, method_of_preparation=bangalore_dosa)
___________ (__CHENNAI__) ___________ (_BANGALORE_)
chennai_dosa
<function __main__.chennai_dosa>
chennai_dosa.__class__
function
def dosa_maker(style):
def chennai(*args, **kwargs):
print args, kwargs
return "Suda suda dosa saar"
def bangalore(*args, **kwargs):
print args, kwargs
return "Bisi Bisi dose saar"
styles = {
"CHENNAI": chennai,
"BANGALORE": bangalore
}
style = style.strip().upper()
return styles[style]
bangalore_dosa_maker = dosa_maker(style="bangalore")
print bangalore_dosa_maker(masala=True)
() {'masala': True} Bisi Bisi dose saar