I have small doubt in python.
x = ''
if x:
print 'y'
so, it will never print as ‘y’
if I do :
x = 0
if x:
print 'y'
Then also it will do the same.
Then how can differentiate if I have ” value and 0 value If I have to consider only 0 values?
That is because the object functions
__nonzero__()and__len__()will be used to evaluate the condition. From the docs:By overloading these you can be used to specify such behavior for custom classes as well.
As for if conditions, you can specify exactly what you want to check:
As for your special case of implicit boolean evaluation:
intobjects are consideredTrueif they are nonzero;strobjects are consideredTrueif their length is nonzero.