So, I was working on some code trying to resolve a bug. This was the original chunk of code:
passrate = 90
for child in sorted_children:
if child.passrate >= passrate:
return child
return None
This code was buggy and this is it's fix:
passrate = 90
for child in sorted_children:
if child.passrate() >= passrate:
return child
return None
The only difference is the added parenthesis. So, child
is a class and passrate()
is it's method which lazy-loads and returns it's __passrate
value. If it's not calculated yet, it calculates it before returning it.
When I used the debugger to see what was causing the problem, I saw that sometimes when passrate()
was executing it was like code execution somehow ended up in a completely wrong instance of child's class.
I know that without the parenthesis a pointer to the function is returned, but as it's done inside a logical operation, the function should be executed immediately afterwards so the final result should be the same for both chunks of code. And sometimes it indeed was. But sometimes it wasn't for some reason, always in the same iterated child in every execution of the code.
If someone could explain what could have caused the problem, I'd appreciate it very much.
EDIT: Thanks everyone for helping. The old code was clearly wrong. I have no idea how it worked at all in the past.
I think, as per python's rule if it's method, then it should be called with braces. If it's a property then you can call without braces as below:
class Hello(object):
@property
def hi(self):
print "hello"
def hifunc(self):
print "Hi function"
h=Hello()
print h.hi
print h.hifunc
print h.hifunc()
Output:
hello
None
<bound method Hello.hifunc of <__main__.Hello object at 0x0000000002B99358>>
Hi function
None
None is printed as my example function returns nothing. In your case, when you call with braces, your return values from function used for comparison.
So, I was working on some code trying to resolve a bug. This was the original chunk of code:
passrate = 90
for child in sorted_children:
if child.passrate >= passrate:
return child
return None
This code was buggy and this is it's fix:
passrate = 90
for child in sorted_children:
if child.passrate() >= passrate:
return child
return None
The only difference is the added parenthesis. So, child
is a class and passrate()
is it's method which lazy-loads and returns it's __passrate
value. If it's not calculated yet, it calculates it before returning it.
When I used the debugger to see what was causing the problem, I saw that sometimes when passrate()
was executing it was like code execution somehow ended up in a completely wrong instance of child's class.
I know that without the parenthesis a pointer to the function is returned, but as it's done inside a logical operation, the function should be executed immediately afterwards so the final result should be the same for both chunks of code. And sometimes it indeed was. But sometimes it wasn't for some reason, always in the same iterated child in every execution of the code.
If someone could explain what could have caused the problem, I'd appreciate it very much.
EDIT: Thanks everyone for helping. The old code was clearly wrong. I have no idea how it worked at all in the past.
I think, as per python's rule if it's method, then it should be called with braces. If it's a property then you can call without braces as below:
class Hello(object):
@property
def hi(self):
print "hello"
def hifunc(self):
print "Hi function"
h=Hello()
print h.hi
print h.hifunc
print h.hifunc()
Output:
hello
None
<bound method Hello.hifunc of <__main__.Hello object at 0x0000000002B99358>>
Hi function
None
None is printed as my example function returns nothing. In your case, when you call with braces, your return values from function used for comparison.
0 commentaires:
Enregistrer un commentaire