In Dropout(0.8)(x), what is (x) meant for? [closed] - python

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.

Related

How can simply this function [closed]

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

How do you create an if statement that doesn't evaluate? [closed]

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.

How do I return a result from a function in Python [closed]

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.

Why python does not update an external parameter with assignment? [closed]

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 7 years ago.
Improve this question
Can anyone explain the reason why python behaves differently in the following two cases please? Much appreciation.
def modifyNone(x):
print("B4:"+str(x))
# x.append(5)
x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[]
Method:
def modifyNone(x):
print("B4:"+str(x))
x.append(5)
# x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[5]
Python is pass by value, so you'll have to reassign a returned value like so:
a = modifyNone(a) # where the function returns a value
You don't seem to understand variable scope as well, try the documentation.

Importing from a function within a function in Python [closed]

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.

Categories