Created
October 25, 2019 23:51
-
-
Save leimao/aad0bfa9eaa842323b6baa850dc54b44 to your computer and use it in GitHub Desktop.
* usages, *args, **kwargs in Python
This file contains 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
# The object after * must be a iterator | |
# * is used to unpack a iterator | |
list_1 = [0,1,2] | |
print(list_1) | |
# Iterate list | |
print(*list_1) | |
print("*"*50) | |
tuple_1 = (0,1,2) | |
print(tuple_1) | |
# Iterate the tuple | |
print(*tuple_1) | |
print("*"*50) | |
dict_1 = {"arg_0":0, "arg_1":1, "arg_2":2} | |
print(dict_1) | |
# Iterate the keys in the dictionary | |
print(*dict_1) | |
# Get the keys in the dictionary as list | |
print([*dict_1]) | |
print("*"*50) | |
def gen_func(n): | |
for i in range(n): | |
yield i | |
# Generator is also an iterator | |
gen_1 = gen_func(3) | |
print(gen_1) | |
# Iterate the generator | |
print(*gen_1) | |
print("*"*50) | |
# *args in Python | |
# You would be very limited by this variable number of arguments | |
# Keyword name would not be supported anymore | |
def func_1(arg_0, arg_1=1000, *args): | |
print(arg_0) | |
print(arg_1) | |
print(args) | |
print(type(args)) | |
func_1(0, 1, 2, 3) | |
# Illegal function calls if keyword is used | |
# func_1(arg_0=0, arg_1=1, arg_2=2) | |
# func_1(arg_0=0, 1, 2) | |
# func_1(0, 1, arg_0=2) | |
print("*"*50) | |
# **kwargs in Python | |
def func_2(arg_0, arg_1=1000, **kwargs): | |
print(arg_0) | |
print(arg_1) | |
print(kwargs) | |
print(type(kwargs)) | |
func_2(0, lion=10000) | |
print("-"*50) | |
func_2(0, 1, lion=10000) | |
print("-"*50) | |
func_2(0, 1, lion=10000, panda=20000) | |
# Illegal function calls | |
# func_2(0, 1, 2, lion=10000) | |
print("*"*50) | |
# Use *args and **kwargs together | |
def func_3(arg_0, arg_1=1000, *args, **kwargs): | |
print(arg_0) | |
print(arg_1) | |
print(args) | |
print(kwargs) | |
func_3(0) | |
print("-"*50) | |
func_3(0, 1) | |
print("-"*50) | |
func_3(0, 1, 2) | |
print("-"*50) | |
func_3(0, lion=10000) | |
print("-"*50) | |
func_3(0, 1, lion=10000) | |
print("-"*50) | |
func_3(0, 1, 2, lion=10000) | |
print("-"*50) | |
func_3(arg_0=0, lion=10000) | |
print("-"*50) | |
func_3(arg_0=0, arg_1=1, lion=10000) | |
print("-"*50) | |
func_3(arg_0=0, arg_1=1, arg_2=2, lion=10000) | |
print("-"*50) | |
func_3(0, lion=10000) | |
print("-"*50) | |
func_3(0, 1, arg_2=2, lion=10000) | |
print("-"*50) | |
func_3(0, 1, 2, arg_2=2, lion=10000) | |
print("*"*50) | |
# Make good use of **kwargs | |
def func_4(arg_0, arg_1=1000, *args, **kwargs): | |
print(arg_0) | |
if "panda" in kwargs: | |
print(kwargs["panda"]) | |
func_4(0, panda=20000) | |
print("-"*50) | |
func_4(0, elephant=30000) | |
print("*"*50) | |
# Some corner usages of * | |
first, *middle, last = [1,2,3,4] | |
print(middle) | |
print(type(middle)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment