Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110427

Re: Break and Continue: While Loops

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Break and Continue: While Loops
Date 2016-06-24 02:23 +1000
Message-ID <mailman.80.1466698995.11516.python-list@python.org> (permalink)
References (1 earlier) <nkgcrh$r90$1@dont-email.me> <CAPTjJmp=nAYjxQ-MnWAaXXEfWq3_FBBtbaugde72XPwydHNLqw@mail.gmail.com> <mailman.69.1466681968.11516.python-list@python.org> <nkh0jj$e4$1@dont-email.me> <CAPTjJmpFzcypEwECu3RAYysq3h8oNgPeDaKftdYYX4UeEabsgQ@mail.gmail.com>

Show all headers | View raw


On Fri, Jun 24, 2016 at 1:52 AM, BartC <bc@freeuk.com> wrote:
> On 23/06/2016 12:39, Chris Angelico wrote:
>>
>> On Thu, Jun 23, 2016 at 8:15 PM, BartC <bc@freeuk.com> wrote:
>>>
>>> Actually pretty much any expression can be used, because Python can
>>> interpret almost anything as either True or False. Don't ask for the
>>> rules
>>> because they can be complicated, but for example, zero is False, and any
>>> other number is True. I think.
>>
>>
>> The rules are very simple. Anything that represents "something" is
>> true, and anything that represents "nothing" is false. An empty
>> string, an empty list, an empty set, and the special "None" object
>> (generally representing the absence of some other object) are all
>> false. A string with something in it (eg "Hello"), a list with
>> something in it (eg [1,2,4,8]), etc, etc, are all true.
>
>
> But even with ordinary conditionals, False is False, but [False] is True.
> And [] is False, while [[]] is True. A class instance is always True, even
> when empty. And then "False" is True as well!

[False] is a list with something in it. So is [[]]. "False" is a
string with something in it, and honestly, if you think that ought to
come out falsey, you're asking for magic. I don't know what you mean
by a "class instance", or how it would be empty, but if you're
creating a custom class and it might sometimes represent emptiness,
you need to define either __len__ or __bool__ so the interpreter can
understand what you mean by "empty".

> Maybe I was thinking of 'is' where sometimes the results are unintuitive.
>

Not at all. The 'is' operator tells you whether the left and right
operands are the same object. That's it. Maybe you have unintuitive
situations where you don't realize you have the same object (or don't
realize you have different objects), but the operator itself still has
one very simple rule to follow.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-22 21:17 -0700
  Re: Break and Continue: While Loops Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-22 21:49 -0700
    Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:08 -0700
  Re: Break and Continue: While Loops Rustom Mody <rustompmody@gmail.com> - 2016-06-22 22:05 -0700
    Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:09 -0700
  Re: Break and Continue: While Loops DFS <nospam@dfs.com> - 2016-06-23 01:20 -0400
    Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:09 -0700
  Re: Break and Continue: While Loops Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-23 18:28 +1000
  Re: Break and Continue: While Loops alister <alister.ware@ntlworld.com> - 2016-06-23 09:53 +0000
  Re: Break and Continue: While Loops BartC <bc@freeuk.com> - 2016-06-23 11:15 +0100
    Re: Break and Continue: While Loops Chris Angelico <rosuav@gmail.com> - 2016-06-23 21:39 +1000
      Re: Break and Continue: While Loops Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-23 11:57 +0000
        Re: Break and Continue: While Loops Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-26 00:32 -0700
          Re: Break and Continue: While Loops Steven D'Aprano <steve@pearwood.info> - 2016-06-26 22:18 +1000
      Re: Break and Continue: While Loops BartC <bc@freeuk.com> - 2016-06-23 16:52 +0100
        Re: Break and Continue: While Loops Chris Angelico <rosuav@gmail.com> - 2016-06-24 02:23 +1000
        Re: Break and Continue: While Loops pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-06-24 09:53 +0200
  Re: Break and Continue: While Loops John Gordon <gordon@panix.com> - 2016-06-23 17:34 +0000
  Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:42 -0700
    Re: Break and Continue: While Loops Quivis <quivis@domain.invalid> - 2016-06-28 23:32 +0000

csiph-web