Note: Won't spend too much time on the body of the functions unless required
In [1]:
def foobar():
print "Foobar"
foobar()
In [2]:
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"
In [3]:
say_hello("Chennaipy", "English")
Out[3]:
In [4]:
say_hello("English", "Chennaipy")
Out[4]:
Which of course means, the order in which they are passed, matters
In [5]:
say_hello(name="Chennaipy", language="Japanese")
Out[5]:
In [6]:
say_hello(language="Japanese", name="Chennaipy")
Out[6]:
Which of course means, the order in which they are passed, doesn't matter
In [7]:
def square(*nums):
print type(nums)
print nums
square(2,3,4,6,7,8,5,4,3,2)
In [8]:
def make_coffee(**config):
print type(config)
print config
make_coffee(decaf=False, milk=True, extra_sugar=True)
In [9]:
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)
In [10]:
def hello_world(name=None):
if name:
return "Hello, " + name
else:
return "Hello, world"
In [11]:
print hello_world()
print hello_world("Chennaipy")
In [12]:
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)
In [13]:
def fn1(arg1, arg2, arg3):
def fn2():
print "from fn2", arg1, arg2, arg3
fn2()
fn1(1,2,3)
In [14]:
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)
In [15]:
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)
In [16]:
chennai_dosa
Out[16]:
In [17]:
chennai_dosa.__class__
Out[17]:
In [18]:
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)