optional parameters in python code example
Example 1: python default arguments
def my_function(name, age = 20):
print(name + " is " + str(age) + " years old"
my_function("Mitro")
my_function("Mitro", 26)
Example 2: default values python
class Test:
def __init__(self, val, num = 0):
self.val = val
self.num = num
t = Test(1)
print(t.num)
t = Test(1, 2)
print(t.num)
Example 3: optional argument python
def myfunc(a,b, *args, **kwargs):
c = kwargs.get('c', None)
d = kwargs.get('d', None)
myfunc(a,b, c='nick', d='dog', ...)
Example 4: optional parameter in python
def to_smash(total_candies, n_friends=3):
return total_candies % n_friends
Example 5: optional arguments python
def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None):