Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can I simply this function:
def fun1(param, value, send):
if send == True:
fun2(**{param:value})
else:
fun2()
You could put the if...else inside the function argument:
def fun1(param, value, send):
return fun2(**({param:value} if send else {}))
Another way to write this function is as follows:
def fun1(param, value, send):
fun2(**{param:value}) if send == True else fun2()
The syntax is:
a if condition_meets else b
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have variables x and y defined, how do I create a stub if statement that evaluates whether x is less than y and does not do anything, even if the condition is true?
This has a condition for if x is less than y and does nothing
if x < y:
pass
pass is used as a placeholder and nothing happens when pass is executed. If you want to learn more about the pass statement, you can check out https://www.programiz.com/python-programming/pass-statement.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Suppose there is a function written in Python
def functionname() -> int:
Should the programmer must return the int in this function? Or is it acceptable to return nothing but use print()?
Should the programmer must return the same data type as the type hint?
It is not necessary to return an int, as in there won't be any error raised if you won't return an int, but if you do not want to return anything or just call a print in the function, it is better to annotate the return type as None and not int.
Edit: If you want it to return None or an int, then you can typehint it as typing.Union[None, int] or typehint.Optional[int]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How do I get the returned result from the below function?
def various_return_types(n):
if(n==1):
return True
else:
return False
various_return_types(1)
The function call does return a value but since you are not assigning it to a variable, you can't use it.
If you use
ret = various_return_types(1)
then, ret will contain the boolean value you can check.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In the following Python code, what does the (x) mean?
Dropout(0.8)(x)
(x) uses the preceding function reference (returned by Dropout(0.8)) to make a function call with x as the argument.
For example:
def Dropout(a):
def func(b):
return a + b
return func
x = 0.2
print(Dropout(0.8)(x))
would output 1.0.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a file (test.py):
def main():
def hello():
x = 10
Then I have another file (test2.py):
from test import *
print(main.hello.x)
Now I know that print(main.hello.x) won't work but I want something like that.
I want to access a variable inside another file that is in a nested function.
How can I do that?
This feels quite unpythonic, but this will work:
def main():
def hello():
hello.x = 10
return hello
main.hello = hello
return main
print(main.hello.x) #returns 10
a more pythonic approach is to use a class.