Hack Bulgaria
github.com/HackBulgaria
Стандарните аргументи, които сме разглеждали до сега, са позиционни
Те зависят от позицията и са задължителни
Това са аргументи подадени на функция в определен ред
def sum_in_range(a, b):
return sum(range(a, b + 1))
print(sum_in_range(1, 4))
>>> 10
def test_var_args(*args):
for arg in args:
print("arg:{}".format(arg))
>>> test_var_agrs(1, "two", 3)
arg: 1
arg: two
arg: 3
def test_var_args(farg, *args):
print("formal arg:{}".format(farg))
for arg in args:
print("another arg:{}".format(arg))
>>> test_var_agrs(1, "two", 3)
formal arg: 1
another arg: two
another arg: 3
Можете да ги срещнете и като пример за variable length arguments
def a(*args):
print("len of arguments: {0}, arguments: {1}".format(len(args), args))
>>> a(1)
len of arguments: 1, arguments: (1,)
>>> a(1, 2, 4)
len of arguments: 3, arguments: (1, 2, 4)
Това са аргументи на функция, които:
def sum_numbers(a, b=7):
return a + b
sum_numbers(5)
12
sum_numbers(5, 6)
11
sum_numbers(5, b=8)
13
sum_numbers(a=5, b=8)
13
sum_numbers(b=6, a=5)
11
sum_numbers(b=6,5)
File "<ipython-input-10-17c4794c3159>", line 1
sum_numbers(b=6,5)
SyntaxError: non-keyword arg after keyword arg
def tomato(weigth, color='red'):
print("Tomato - {}gr".format(weigth))
print("And it is colored {}".format(color))
tomato(20) # 1 positional argument
tomato(weigth=1000) # 1 keyword argument
ttomato(10, color='pink') # 1 positional argument, 1 keyword
tomato(weigth=25, color='yellow')# 2 keyword arguments
Tomato - 20gr
And it is colored red
Tomato - 1000gr
And it is colored red
Tomato - 10gr
And it is colored pink
Tomato - 25gr
And it is colored yellow
>>> tomato(20)
>>> tomato(weigth=1000)
>>> tomato(10, color='pink')
>>> tomato(weigth=25, color='yellow')
>>> tomato()
>>> tomato(color='pink')
>>> tomato(20, 'black')
>>> tomato(weigth=10, 'blue')
tomato()
TypeError: tomato() missing 1 required positional argument: 'weigth'
tomato(color='pink')
TypeError: tomato() missing 1 required positional argument: 'weigth'
tomato(20,'black')
Tomato - 20gr
And it is colored black
tomato(weigth=10,'blue')
^
SyntaxError: non-keyword arg after keyword arg
def test_var_kwargs(farg, **kwargs):
print("formal arg:{}".format(farg))
for key in kwargs:
print ("another keyword arg: {}={}".format(key, kwargs[key]))
>>> test_var_kwargs(farg=1, myarg2="two", myarg3=3)
formal arg:1
another keyword arg: myarg3=3
another keyword arg: myarg2=two
def cheeseshop(kind, *args, **kwargs):
print("-- Do you have any {}".format(kind))
for arg in args:
print(arg)
print("-"*40)
for key in kwargs:
print("{}:{}".format(key, kwargs[key]))
cheeseshop("Limburger", "It's very runny, sir.", "It's really VERY runny",
shopkeeper="Michael", client="John", sketch="Cheese Shop Sketch")
>>> cheeseshop("Limburger", "It's very runny, sir.","It's really VERY runny",
shopkeeper="Michael", client="John", sketch="Cheese Shop Sketch")
-- Do you have any Limburger
It's very runny, sir.
It's really VERY runny
----------------------------------------
sketch:Cheese Shop Sketch
shopkeeper:Michael
client:John
git init <directory>
git clone <repo> <directory>
git config --global user.name <name>
git config --system core.editor <editor>
git add <file> | <directory> | -p | -A
git commit -a | -m
git fetch <remote>
git branch -r
git pull | origin <branch_name>
git checkout <branch_name>
git merge <branch_name>
By Hack Bulgaria