+ 5
Can anyone explain this code?
def func1(): Pass def func2(n): if n%2==0: return none print(func1() == func2(6)) print(func1() == func2(3)) https://code.sololearn.com/cLrH4cJd6NS0/?ref=app Answer: true , true But how???????
6 Answers
+ 6
'func2' is defining like that :
def func2(n) :
if n%2 == 0:
return None
else:
return None
And your first function is the same as :
def func1() :
return None
print(func1()) # None
print(func2(2)) # None
print(func2(1)) # None
As you can see, your two functions are returning None, whatever happens.
>>> None == None
True
So, 'func1() == func2(a_value)' will be True in any case.
+ 6
ThĂ©ophile Thanks! Now I understood đ€
+ 4
An empty return statement is equal to :
'return None'
If a function does not return any value, it returns None.
import dis
def func():
pass
did.dis(func)
You can see :
LOAD_CONST 0 (None)
RETURN_VALUE
+ 4
Théophile
func1() is already return none
but in the last line,
3 is argument of func2()
And in the func2() , there is a if statement
if 3%2==0 it's return none
And if this statement is true
Then,
Last line condition will become true and it prints true
But in the line no. 4 ,
3%2==1
So it condition will become false
But answer is true
but how?????
+ 4
Théophile
But in line no. 4,
3%2==1
So condition will become false
+ 2
The call 'func2(3)' will return None. The reason is simple : if n is not even, then the function returns nothing... So it returns None.
def func(a):
if a == 3:
return None
print(func(3)) # None
print(func(1)) # None
[EDIT]
'func1' will always return None, like 'func2'. That's why the result is always True.