"vectors" as arguments of functions

In [9]:
import numpy as np
In [10]:
def f(*argv):
    x=argv[0]
    y=argv[1]
    z=argv[2]
    return x+y+z
In [11]:
f(*[0,1,2])
Out[11]:
3
In [12]:
f(*[0,1,2,4])
Out[12]:
3
In [17]:
def fArgs( *arguments):
    res=0
    for arg in arguments:
        res+=arg
    return res
In [19]:
fArgs(*[0,1,2,4])
Out[19]:
7
In [20]:
def fNdim(*argv):
    return np.sum(np.array(argv))
In [22]:
fNdim(*[0,1,2,4])
Out[22]:
7

Dictionaries as inputs for functions

In [ ]:
d={"orario":1600,"b":2}
In [56]:
d.keys()
Out[56]:
dict_keys(['orario', 'b'])
In [5]:
d['orario']
Out[5]:
1600
In [52]:
d[0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-52-123a9cc6df61> in <module>()
----> 1 d[0]

KeyError: 0
In [53]:
l=[12,34,0,33]
In [54]:
l[0]
Out[54]:
12
In [24]:
def fDict(**keywords):
    for kw in keywords:
        print(kw, ":", keywords[kw])
In [27]:
fDict(**{"a":1,"b":2})
a : 1
b : 2
In [6]:
def fDictX(x,**keywords):
    for kw in keywords:
        print(kw, ":", keywords[kw])
    return keywords['a']+x*keywords['b']
In [7]:
fDictX(0.1,**{"a":1,"b":2})
a : 1
b : 2
Out[7]:
1.2

Define optional arguments $x$ and $y$

In [31]:
def fOptional(x=1,y=2):
    print(x*y)
In [32]:
fOptional()
2
In [33]:
fOptional(x=3)
6

If $gDict$ and $fDictX$ share some arguments I can pass them along as a single "box" object

In [57]:
def gDict(**keywords):
    # do ...
    return fDictX(2,**keywords)
In [58]:
gDict(**{'a':'first','b':'second'})
a : first
b : second
Out[58]:
'firstsecondsecond'

I can pass arguments from the outer function to the inner one

In [43]:
def gOptional(**kwarg):
    return fOptional(**kwarg)
In [59]:
gOptional(x=2,y=5,z=1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-59-540b638d1f52> in <module>()
----> 1 gOptional(x=2,y=5,z=1)

<ipython-input-43-637f580e879e> in gOptional(**kwarg)
      1 def gOptional(**kwarg):
----> 2     return fOptional(**kwarg)

TypeError: fOptional() got an unexpected keyword argument 'z'
In [48]:
def hOptional(debug=True,**kwarg):
    print(debug)
    return fOptional(**kwarg)
In [50]:
hOptional(debug=None)
None
2
In [51]:
hOptional(debug=1,x=55)
1
110