Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2711 > unrolled thread
| Started by | "eryksun ()" <eryksun@gmail.com> |
|---|---|
| First post | 2011-04-06 09:58 -0700 |
| Last post | 2011-04-06 20:44 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Re: TypeError: iterable argument required "eryksun ()" <eryksun@gmail.com> - 2011-04-06 09:58 -0700
Re: TypeError: iterable argument required Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2011-04-06 12:21 -0700
Re: TypeError: iterable argument required MRAB <python@mrabarnett.plus.com> - 2011-04-06 20:44 +0100
| From | "eryksun ()" <eryksun@gmail.com> |
|---|---|
| Date | 2011-04-06 09:58 -0700 |
| Subject | Re: TypeError: iterable argument required |
| Message-ID | <c5d42146-03fd-431d-982e-9d221faf4c91@glegroupsg2000goo.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2011-04-06 12:21 -0700 |
| Message-ID | <0ad45930-e3aa-4ad9-a085-49a2d7ddaa1f@s3g2000vbf.googlegroups.com> |
| In reply to | #2711 |
On 6 Απρ, 19:58, "eryksun ()" <eryk...@gmail.com> wrote: > 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. I doesnt matter if *y* is True or False before its value is returned? *y*'s value returned no matter if its true or false? If we were to describe it in english words how would we describe the expression `x or y`? x = True or y = True? >Since 'mail is None' and None evaluates to False What does the expression "None evaluates to false" mean in simpler words? Every expression is evaluated at the end as True or False?
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-04-06 20:44 +0100 |
| Message-ID | <mailman.83.1302119066.9059.python-list@python.org> |
| In reply to | #2715 |
On 06/04/2011 20:21, Νικόλαος Κούρας wrote:
> On 6 Απρ, 19:58, "eryksun ()"<eryk...@gmail.com> wrote:
>
>> 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.
>
> I doesnt matter if *y* is True or False before its value is returned?
> *y*'s value returned no matter if its true or false?
>
> If we were to describe it in english words how would we describe the
> expression `x or y`?
> x = True or y = True?
>
>> Since 'mail is None' and None evaluates to False
>
> What does the expression "None evaluates to false" mean in simpler
> words?
> Every expression is evaluated at the end as True or False?
For `x or y`, if `bool(x)` is True, it returns `x`, else it returns `y`.
For `x or y or z`, if `bool(x)` is True, it returns `x`, else if
`bool(y)` is True, it returns `y`, else it returns `z`.
And so on.
In Python, the convention is for certain things to be regarded as False
(bool(thing) returns False) and everything else to be regarded as True
(bool(thing) returns True).
'False' things include None, empty strings, empty containers (empty
list, etc), and zero.
'True' things include non-empty strings, non-empty containers, and
non-zero numbers.
When in doubt, ask Python:
>>> bool([])
False
>>> bool("Hello world!")
True
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web