Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56545
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-10-09 23:12 -0700 |
| References | <91180c35-413f-4b65-a224-917d8d68b7ec@googlegroups.com> |
| Message-ID | <a471eecd-2e9f-4d32-8825-bdc088a0ecb0@googlegroups.com> (permalink) |
| Subject | Re: Python's and and Pythons or |
| From | Peter Cacioppi <peter.cacioppi@gmail.com> |
On Wednesday, October 9, 2013 4:54:03 PM UTC-7, Peter Cacioppi wrote: > I really like the logic that Pythons "or" is not only short-circuit but non-typed. > > > > So I can say > > > > y = override or default > > > > and y won't necc be True or False. If override boolean evaluates to True (which, for most classes, means not None) than y will be equal to override. Otherwise it will be equal to default. > > > > I have two questions > > --> Is there a handy name for this type of conditional (something as catchy as "short circuit or") > > > > and > > > > --> Is there a common idiom for taking advantage of the similar behavior of "and". The "override or default" just makes me grin every time I use it. > > > > Thanks ok, since someone asked, I suggest we call the "return it's arguments" behavior "echo-argument". That is to say, the reason we can write y = override or default is because Python implements echo-argument or. That is to say, "or" doesn't necc return True or False, "or" returns the first "truthy" argument it encounters. "and" behaves similarly, in that it returns the first "falsey" argument it encounters. I'm trying to think of a good example usage of echo-argument and. Maybe something like possible = foo and foo.allowsit() if (possible is None) : print "foo not provided" if (possible is False) : print "foo doesn't allow it" A bit awkward, echo-argument or is more naturally useful to me then echo-argument and.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python's and and Pythons or Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-09 16:54 -0700
Re: Python's and and Pythons or Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-10 00:36 +0000
Re: Python's and and Pythons or Chris Angelico <rosuav@gmail.com> - 2013-10-10 12:13 +1100
Re: Python's and and Pythons or Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-09 23:12 -0700
Re: Python's and and Pythons or Chris Angelico <rosuav@gmail.com> - 2013-10-10 17:45 +1100
Re: Python's and and Pythons or Ethan Furman <ethan@stoneleaf.us> - 2013-10-09 23:46 -0700
Re: Python's and and Pythons or Terry Reedy <tjreedy@udel.edu> - 2013-10-10 03:43 -0400
Re: Python's and and Pythons or Chris Angelico <rosuav@gmail.com> - 2013-10-10 19:03 +1100
Re: Python's and and Pythons or Ethan Furman <ethan@stoneleaf.us> - 2013-10-10 06:33 -0700
Re: Python's and and Pythons or Terry Reedy <tjreedy@udel.edu> - 2013-10-10 21:41 -0400
Re: Python's and and Pythons or Ethan Furman <ethan@stoneleaf.us> - 2013-10-10 19:47 -0700
Re: Python's and and Pythons or Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-09 23:55 -0700
csiph-web