def
f
(a, L
=
None
):
if
L
is
None
:
L
=
[]
L
.
append(a)
return
L
Οι συναρτήσεις μπορούν επίσης να κληθούν με τη χρήση
της keyword arguments της μορφής kwarg = value. Για
παράδειγμα, η ακόλουθη συνάρτηση:
def
parrot
(voltage, state
=
'a stiff'
, action
=
'voom'
,
type
=
'Norwegian Blue'
):
(
"-- This parrot wouldn't"
, action, end
=
' '
)
(
"if you put"
, voltage,
"volts through it."
)
(
"-- Lovely plumage, the"
,
type
)
(
"-- It's"
, state,
"!"
)
δέχεται ένα απαιτούμενο όρισμα (voltage) και τρία
προαιρετικά επιχειρήματα (state, action, type). Αυτή η
συνάρτηση μπορεί να καλείται με οποιοδήποτε από τους
ακόλουθους τρόπους:
parrot(
1000
)
# 1
positional argument
parrot(voltage
=
1000
)
# 1
keyword argument
parrot(voltage
=
1000000
, action
=
'VOOOOOM'
)
# 2
keyword arguments
parrot(action
=
'VOOOOOM'
, voltage
=
1000000
)
# 2
keyword arguments
parrot(
'a million'
,
'bereft of life'
,
'jump'
)
# 3
positional arguments
parrot(
'a thousand'
, state
=
'pushing up the daisies'
)
# 1
positional, 1 keyword
- A Mini greek notebook for Learning Python Programming
26