i'm trying to update a default value in a combobox, after a function is called successfully, how could i do that?
my basic idea is this.
if xfunc return True : widget.set(updated_value)
return is a keyword that is used to return objects during runtime in a function, meaning if you write the word 'return' anywhere in the function (main or otherwise): the compiler will try to end the function with whatever follows as the return type. Therefore, when writing if statements: in your mind try to replace the function call 'xfunc()' with whatever you think it will return, and test to see if it is equal ('==') with the return you want ('True')
However, the job of a if-statement is to test if something is True. Therefore if xfunc() == True: would be redundant (as pointed out by all or None), so we can simplify this statement to if xfunc():
ex:
if xfunc():
#run success code here
Related
Why is the following code resulting in just True? Shouldn't the output be something like this:
True
False
because there's no else statement. So once it verifies and executes the if command, shouldn't it read thereturn False command as well? This command is not within the If indentation and it's not even mentioned with else.
def is_even(number):
if number % 2 == 0:
return True
return False
print(is_even(2))
Executing 'return' terminates function execution immediately, with the specified value as the value of the function call.
It does not mean something like 'add this to what is to be returned at some future time, meanwhile carry on execution', which is what would be needed for your apparently understanding.
This is how 'return' works in most procedural languages.
return statement is used to return a value to the caller and exit a function. A function will stop immediately and return a value if it meets with return statement.
Also note that if statement does not requires else clause every time. It can stay alone to handle specific cases.
As an example:
x = 44
if x>46:
print("hello")
print("hi")
The below works and gives 8:
def mypow(a,b):
c=a**b
print (c)
mypow(2,3)
But the below doesn't:
def mypow(a,b):
c=a**b
mypow(2,3)
I understand that in the former we print but since the latter doesn't give an error message (nor does it give a result), what is actually happening in the latter?
in
def mypow(a,b):
c=a**b
mypow(2,3)
after function executed; all data inside the function destroy. if you want to see anything on your screen after function is executed; either use print or return.
In the end of any function you have multiple choices.
1- return. return the output. when you return the output you receive it after function executed without error eg: d = func(any_value), if you specified the return value in func you will receive it and store it in d. if you don't specify the return statement in the function the function will return None.
2- print. print anything. this is what you did in your first function. in this case you get printed stuff in your screen, and you function return None, so you can't hold the output (a printed value) in any variable or use it anywhere else.
3- global. assign to global. when you need this option you create a variable outside of your function say my_variale=5, and in the first line of your function you write gloabl my_variable. say the function contain 5 lines, in line #3 you did my_variable=7. after function is executed and destroyed all values within the function; when you check my_variable you will see it contains value of 5.
You can replace print with return for your function and get the same result
def mypow(a,b):
c=a**b
return c
mypow(2,3)
Variables nested in functions are treated as local variables unless you add global before the variable name.
I am looking for a way in python to stop certain parts of the code inside a function but only when the output of the function is assigned to a variable. If the the function is run without any assignment then it should run all the inside of it.
Something like this:
def function():
print('a')
return ('a')
function()
A=function()
The first time that I call function() it should display a on the screen, while the second time nothing should print and only store value returned into A.
I have not tried anything since I am kind of new to Python, but I was imagining it would be something like the if __name__=='__main__': way of checking if a script is being used as a module or run directly.
I don't think such a behavior could be achieved in python, because within the scope of the function call, there is no indication what your will do with the returned value.
You will have to give an argument to the function that tells it to skip/stop with a default value to ease the call.
def call_and_skip(skip_instructions=False):
if not skip_instructions:
call_stuff_or_not()
call_everytime()
call_and_skip()
# will not skip inside instruction
a_variable = call_and_skip(skip_instructions=True)
# will skip inside instructions
As already mentionned in comments, what you're asking for is not technically possible - a function has (and cannot have) any knowledge of what the calling code will do with the return value.
For a simple case like your example snippet, the obvious solution is to just remove the print call from within the function and leave it out to the caller, ie:
def fun():
return 'a'
print(fun())
Now I assume your real code is a bit more complex than this so such a simple solution would not work. If that's the case, the solution is to split the original function into many distinct one and let the caller choose which part it wants to call. If you have a complex state (local variables) that need to be shared between the different parts, you can wrap the whole thing into a class, turning the sub functions into methods and storing those variables as instance attributes.
Why do I have to return function in the else case? Can't I just apply the defined function because I have to only return the value of b and store it?
def gcd_a(a,b):
if a==0:
return b
else:
gcd_a(b%a,a)
I think the main concept you are missing is that in order to get the result of a recursive function (or any function for that matter), the function that you access the result of must return the value*
Right now, when you call gcd_a, a recursive call to gcd_a will eventually return a value, however it will be lost, since the function that you are accessing the result of does not return a value.
To show you this is true, let's add in a log statement that prints when the function is returning:
def gcd_a(a,b):
if a==0:
print('finally returning!', b)
return b
else:
gcd_a(b%a,a)
Now if we call:
print(gcd_a(10, 99))
We get the following output:
finally returning! 1
None
Somewhere, a recursive call to gcd_a found your condition to be True, and returned 1, however, this result is not printed, because it is not returned by your call to gcd_a
* Unless you do something strange like updating a global variable, etc...
If your function finishes without returning anything, it will implicitly return None.
def double(val):
val * 2
value = double(10)
print(value)
Which is fine, except it doesn't have a return statement, and so all you get is None
The same applies to recursive functions: you can do all the recursive function calls you want, but if you don't actually return the result of the function, then it'll return None.
IMHO, the problem is in the way you are thinking about function which has to be cleared. The fact that you are having this doubt in a recursive function call is incidental because you are calling the same function again and again with different arguments causing different branches to be executed.
How will a function that doesn't return anything in any of its branches be helpful to its caller? In your case, what would happen if your function's caller calls it with an argument that hits your else block? It will return nothing and this won't help the caller!
Now in your recursive case, if your caller calls the function with an argument that hits the if block, then it would work as expected. However, if it hits the else block, then it would become a caller and call the same function again. For simplicity, let us assume that this time it hits the if condition and returns something to its caller. However, will that reach the original caller that initiated everything? The answer is no because you are not returning it to him!
Most of the times you would need a return for every branch in a function
unless you are doing it on purpose for a side effect.
In the else block, if you don't return the function call, the outer function returns None because python interpreter just runs whatever is returned by the gcd function.
Let's assume the following code:
def func():
10
In that function, it just runs 10, but it doesn't mean that there is some return from the function.
I'm viewing the source code of a project now. In which I saw a function like this.
def func(x):
if condition_a:
return
if condition_b:
return
process_something
What does this return with nothing do here?
The "return with nothing" exits the function at that line and returns None. As Python's docs say:
If an expression list is present, it is evaluated, else None is substituted.
return leaves the current function call with the expression list (or None) as return value.
In flow control, I've seen it commonly used when some sort of condition is encountered that makes it impossible to execute the rest of the function; for example,
def getDataFromServer():
if serverNotResponding():
return
# do important stuff here which requires the server to be running