Python&Django: What does ** mean [duplicate] - python

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 6 years ago.
Possibly i know the following uses of **
Power
x ** y # x power y equivalent to pow(x,y)
Passing indefinite number of args
def sample(x, **other):
print(x, other.keys)
sample(x=2,y=3,z=4)
But i don't understand when its used as follow( in Serializers)
def create(self, validated_data):
return Comment(**validated_data)
Can someone give me a hint of what is happening there

It is the opposite of your second example. When in the definition of a function, the ** operator gathers all the named arguments and makes a dictionary. When calling a function, it takes a dictionary and breaks it into named arguments
So, if you have
values = {'x': 1, 'y': 2}
f(**values)
it is the equivalent of
f(x=1, y=2)

Related

How to call another function from a function [duplicate]

This question already has answers here:
How do you call a function in a function?
(4 answers)
Closed 2 years ago.
My First function is addition of program
def addn(x,y):
return (x + y)
My second function is output got from the first function + 2
def addn2(x,y)
return (x+y+2)
Instead of above searching for anything like below, with out re writing everything in the second function
def addn3(x,y):
return (addn + 2)
The parameters are important:
def addn3(x, y):
return (addn(x, y) + 2)

How to writing a function in Python to call another function while only assigning value to some arguments? [duplicate]

This question already has answers here:
Python Argument Binders
(7 answers)
Closed 3 years ago.
Or equivalently, to pass multiple arguments to a called function?
So I am trying to do:
define a function fun1
define another function fun2, which will call fun1 but only assigning some values to fun1, with other values being assigned otherwise; or maybe I can assign multiple arguments to a called function?
def fun1(x, y):
return x*y
def fun2(fun, x):
print(fun(x))
fun2(fun1(y=2), 3) # doesn't work
fun2(fun1, (3, 2)) # doesn't work either
So how should I do this? I want fun2 to just print fun1's result with some variable for x (here I choose it to be 3) and assigned(fixed) y=2
As suggested by #kaya3
Use python's variable *args feature.
IDLE :
>>> def fun1(x, y):
return x * y
>>> def fun2(fun, x):
print(fun(*x))
>>> fun2(fun1, (3, 2))
6

What does * represent in function argument list in python? [duplicate]

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
understanding '*' "keyword only" argument notation in python3 functions [duplicate]
(2 answers)
Closed 3 years ago.
While going through the source code, I noticed the following syntax being used in the asyncio library:
#coroutine
def sleep(delay, result=None, *, loop=None):
"""Coroutine that completes after a given time (in seconds)."""
if delay == 0:
yield
return result
if loop is None:
loop = events.get_event_loop()
future = loop.create_future()
h = future._loop.call_later(delay,
futures._set_result_unless_cancelled,
future, result)
try:
return (yield from future)
finally:
h.cancel()
what does the * do in the argument list?
It means that parameter(s) that comes after * are keyword only parameters.
Consider the following:
def test(delay, result=None, *, loop=None):
print(delay, result, loop)
In this case, test(1,2,2) will raise TypeError since it is expecting at most two positional arguments, i.e. delay and result:
test(1,2,2)
TypeError: test() takes from 1 to 2 positional arguments but 3 were given
The third argument, or loop, can only be assigned if used as keyword:
test(1,2,loop=2)
# 1 2 2
# Works fine
For more detail, refer to Function Definitions

Is there any functional difference between return x,y and return (x,y)? Or is it purely syntactical? [duplicate]

This question already has answers here:
When are parentheses required around a tuple?
(3 answers)
Closed 3 years ago.
So, for example, is there any actual difference between:
def test1():
x = 1
y = 2
return x,y
And:
def test2():
x = 1
y = 2
return (x,y)
Both are valid constructors of the builtin tuple type, so, yes, the difference is purely a different syntax for the same thing.

* mark in print function call [duplicate]

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 6 years ago.
What is this star mark in this print function call?
for i in range(int(input())):
s=input()
print(*["".join(s[::2]),"".join(s[1::2])])
It is called argument unpacking. If you were to omit it, then it would only give the list created by the list comprehension to the print function as one argument. With the asterisk it passes every item in that list as separate argument.
Consider this example:
def my_func(arg1, arg2, arg3):
print('yay it worked')
and then call it with:
my_func(*[1, 2, 3])
that way arg1 will be 1, arg2 will be 2 and arg3 will be 3.
If you change the call to:
my_func([1, 2, 3])
then you pass the list to arg1 and it will raise a TypeError because it's missing two positional arguments.

Categories