Example 1: args kwargs python
>>> def argsKwargs(*args, **kwargs):
... print(args)
... print(kwargs)
...
>>> argsKwargs('1', 1, 'slgotting.com', upvote='yes', is_true=True, test=1, sufficient_example=True)
('1', 1, 'slgotting.com')
{'upvote': 'yes', 'is_true': True, 'test': 1, 'sufficient_example': True}
Example 2: use of kwargs and args in python classes
def myFun(*args,**kwargs):
print("args: ", args)
print("kwargs: ", kwargs)
myFun('my','name','is Maheep',firstname="Maheep",lastname="Chaudhary")
Example 3: **kwargs
When it iterating over a dictionary you are only able to iterate over
the keys not the values. The ** when placed before a variable will allow
you to iterate and unpack both key and value pairs. Because you are
unpacking both key and value this will return the result as a dictionary.
Example 4: python *args
def concatenate(**kwargs):
result = ""
for arg in kwargs:
result += arg
return result
print(concatenate(a="Real", b="Python", c="Is", d="Great", e="!"))
Example 5: python *arg **kwargs
>>> def f(a, b, *args, **kwargs):
... print(F'a = {a}')
... print(F'b = {b}')
... print(F'args = {args}')
... print(F'kwargs = {kwargs}')
...
>>> f(1, 2, 'foo', 'bar', 'baz', 'qux', x=100, y=200, z=300)
a = 1
b = 2
args = ('foo', 'bar', 'baz', 'qux')
kwargs = {'x': 100, 'y': 200, 'z': 300}
Example 6: what are args and kwargs in python
def info(**kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
info(first ='This', mid ='is', last='Me')