TypeError: iterable argument required

eryksun () eryksun at gmail.com
Wed Apr 6 12:58:03 EDT 2011


On Wednesday, April 6, 2011 11:57:32 AM UTC-4, Νικόλαος Κούρας wrote:
> >>> mail = None
> >>> mail = mail or 7
> >>> mail
> 7

Quote:
The expression ``x or y`` first evaluates *x*; if *x* is 
true, its value is returned; otherwise, *y* is evaluated 
and the resulting value is returned.

Since 'mail is None' and None evaluates to False, the operation returns the right-hand operand, 7.

> >>> mail = None
> >>> mail = 7 or mail
> >>> mail
> 7
> 
> Here no matter the order iam writing the comparison it always return
> the number.

In this case the number 7 evaluates to True.

> why not the same here?
> 
> >>> mail = None
> >>> mail = mail or ''
> >>> mail
> ''
> >>> mail = None
> >>> mail = '' or mail
> >>> mail
> >>>
> 
> Why the or operator behaves differently with numbers than from
> strings?

It's behaving the same. You're overlooking the fact that the empty string is False:

In [1]: bool('')
Out[1]: False

Length zero sequences are normally False, but you can override this in a subclass by implementing the __nonzero__ method:

In [2]: class mystr(str):
   ...:     def __nonzero__(self):
   ...:         return True

In [3]: bool(mystr(''))
Out[3]: True




More information about the Python-list mailing list