Created
July 5, 2018 17:14
-
-
Save santosh/90623d046b53f6aff53617a87862f0a0 to your computer and use it in GitHub Desktop.
*args and **kwargs use cases.. #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
def arguments(*args): | |
for i in args: | |
print(i) | |
l = [1, 2, 3, 5] | |
# arguments(1, 2, 3, 5) | |
# arguments(l) | |
# arguments(*l) | |
# one '*' basically flattens a list | |
# '**' basically flattens a dictionary | |
def keyword_arguments(**kwargs): | |
for i in kwargs.items(): | |
print(i) | |
# keyword_arguments(x=123, y=7) | |
shop = {'ciggy': 1, 'chocolate':1 } | |
keyword_arguments(**shop) | |
def both(*args, **kwargs): | |
for arg in args: | |
print(arg) | |
for item in kwarsg.items(): | |
print(item) | |
# you can now pass unlimited number of arguments | |
# make sure args comes in start of than kwargs | |
both(1, 2, 3, 5, x=3, y=8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment