Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79415 > unrolled thread
| Started by | "ast" <nomail@invalid.com> |
|---|---|
| First post | 2014-10-22 10:29 +0200 |
| Last post | 2014-10-24 11:31 -0700 |
| Articles | 17 on this page of 77 — 23 participants |
Back to article view | Back to comp.lang.python
(test) ? a:b "ast" <nomail@invalid.com> - 2014-10-22 10:29 +0200
Re: (test) ? a:b buscacio@gmail.com - 2014-10-22 02:05 -0700
Re: (test) ? a:b "ast" <nomail@invalid.com> - 2014-10-22 11:14 +0200
Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-22 12:45 +0100
Re: (test) ? a:b "ast" <nomail@invalid.com> - 2014-10-27 09:01 +0100
Re: (test) ? a:b Michael Torrie <torriem@gmail.com> - 2014-10-22 11:16 -0600
Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-23 11:44 +1100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-22 20:15 +1100
Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-22 10:16 +0100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-22 20:27 +1100
Re: (test) ? a:b Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-23 16:46 +1300
Re: (test) ? a:b MRAB <python@mrabarnett.plus.com> - 2014-10-23 11:12 +0100
Re: (test) ? a:b "ast" <nomail@invalid.com> - 2014-10-24 08:38 +0200
Re: (test) ? a:b Michael Torrie <torriem@gmail.com> - 2014-10-25 19:04 -0600
Re: (test) ? a:b Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-27 22:04 +1300
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 12:20 +1100
Re: (test) ? a:b Michael Torrie <torriem@gmail.com> - 2014-10-25 21:05 -0600
Re: (test) ? a:b Ned Batchelder <ned@nedbatchelder.com> - 2014-10-22 07:30 -0400
Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-22 14:41 +0300
Re: (test) ? a:b Rustom Mody <rustompmody@gmail.com> - 2014-10-22 06:16 -0700
Re: (test) ? a:b "BartC" <bc@freeuk.com> - 2014-10-22 19:08 +0100
Re: (test) ? a:b Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-22 19:55 -0400
Re: (test) ? a:b "BartC" <bc@freeuk.com> - 2014-10-23 11:50 +0100
Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-23 16:44 +0300
Re: (test) ? a:b Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-23 08:24 -0600
Re: (test) ? a:b Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-10-23 16:41 +0200
Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-23 19:20 +0300
Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-24 19:38 +1100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-24 19:45 +1100
Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-24 12:37 +0300
Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-25 00:07 +1100
Re: (test) ? a:b Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-24 12:22 -0600
Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-24 21:46 +0300
Re: (test) ? a:b Dan Stromberg <drsalists@gmail.com> - 2014-10-24 10:20 -0700
Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-24 20:58 +0000
Re: (test) ? a:b Marko Rauhamaa <marko@pacujo.net> - 2014-10-25 00:02 +0300
Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-25 16:03 +1100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-25 16:25 +1100
Re: (test) ? a:b Ned Batchelder <ned@nedbatchelder.com> - 2014-10-25 07:58 -0400
Re: (test) ? a:b Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-25 07:53 -0600
Re: (test) ? a:b Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-25 14:11 -0400
Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-25 19:49 +0000
Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 07:45 +1100
Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-26 11:10 +1100
Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 12:03 +1100
Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-26 15:15 +1100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 15:34 +1100
Re: (test) ? a:b Terry Reedy <tjreedy@udel.edu> - 2014-10-26 17:12 -0400
Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-27 11:38 +1100
Re: (test) ? a:b MRAB <python@mrabarnett.plus.com> - 2014-10-27 01:40 +0000
Re: (test) ? a:b Rustom Mody <rustompmody@gmail.com> - 2014-10-26 19:19 -0700
Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 12:10 +1100
Re: (test) ? a:b Joshua Landau <joshua@landau.ws> - 2014-10-27 01:55 +0000
Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-27 13:28 +1100
Re: (test) ? a:b Rustom Mody <rustompmody@gmail.com> - 2014-10-26 19:38 -0700
Re: (test) ? a:b Joshua Landau <joshua@landau.ws> - 2014-10-27 02:43 +0000
Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-27 13:56 +1100
Re: (test) ? a:b Dan Stromberg <drsalists@gmail.com> - 2014-10-25 15:34 -0700
Re: (test) ? a:b Ben Finney <ben+python@benfinney.id.au> - 2014-10-26 10:53 +1100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 11:01 +1100
Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-26 01:09 +0100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-26 11:13 +1100
Re: (test) ? a:b Vito De Tullio <vito.detullio@gmail.com> - 2014-10-23 06:55 +0200
Re: (test) ? a:b Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-23 03:28 +1100
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-23 03:40 +1100
Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-22 18:03 +0000
Re: (test) ? a:b Matthew Ruffalo <mmr15@case.edu> - 2014-10-22 14:27 -0400
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-23 05:30 +1100
Re: (test) ? a:b Ned Batchelder <ned@nedbatchelder.com> - 2014-10-22 16:11 -0400
Re: (test) ? a:b Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-22 12:41 +0100
Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-22 15:12 +0000
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-23 02:18 +1100
Re: (test) ? a:b alister <alister.nospam.ware@ntlworld.com> - 2014-10-22 15:24 +0000
Re: (test) ? a:b Dan Stromberg <drsalists@gmail.com> - 2014-10-22 09:09 -0700
Re: (test) ? a:b Tobiah <toby@tobiah.org> - 2014-10-24 10:23 -0700
Re: (test) ? a:b Chris Angelico <rosuav@gmail.com> - 2014-10-25 04:27 +1100
Re: (test) ? a:b Tobiah <toby@tobiah.org> - 2014-10-24 11:31 -0700
Page 4 of 4 — ← Prev page 1 2 3 [4]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-10-26 01:09 +0100 |
| Message-ID | <mailman.15181.1414282207.18130.python-list@python.org> |
| In reply to | #79654 |
On 26/10/2014 01:01, Chris Angelico wrote:
> On Sun, Oct 26, 2014 at 10:53 AM, Ben Finney <ben+python@benfinney.id.au> wrote:
>> Dan Stromberg <drsalists@gmail.com> writes:
>>
>>> EG, if I have 3 mutually exclusive command line options, I'll do
>>> something like:
>>>
>>> if option_a + option_b + option_c != 1:
>>> sys.stderr.write('{}: -a, -b and -c are mutually exclusive\n'.format(sys.argv[0]))
>>
>> That is an excellent illustration of why exploiting this accidental
>> property of True and False leads to obfuscated code. The above test
>> gives me no clue that we're operating on boolean values, nor that we're
>> testing for exclusive options.
>
> Would it be more readable thus?
>
> if len([opt for opt in (option_a, option_b, option_c) if opt]) != 1:
>
> Because that's what it's doing. I think treating bools as numbers is
> fine. Matter of opinion I guess.
>
> ChrisA
>
Horrible IMHO, it just doesn't fit in my mind set. Still each to their own.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-26 11:13 +1100 |
| Message-ID | <mailman.15182.1414282391.18130.python-list@python.org> |
| In reply to | #79654 |
On Sun, Oct 26, 2014 at 11:09 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > Horrible IMHO, it just doesn't fit in my mind set. Still each to their own. Yeah, the comprehension version is way more explicit (though it probably ought to be a set and a set comp, not a tuple and a list comp), and not as good, IMO. But it explains and justifies the original. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Vito De Tullio <vito.detullio@gmail.com> |
|---|---|
| Date | 2014-10-23 06:55 +0200 |
| Message-ID | <mailman.15101.1414040154.18130.python-list@python.org> |
| In reply to | #79464 |
Dennis Lee Bieber wrote: >> x = [f(), g()] [cond] >> >>the latter evaluates both f() and g() instead of just one. Apart from >>being inefficient, it can have unintended side-effects. > > Ah, but what would > > x = [f, g][cond]() > > produce? headache -- By ZeD
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-10-23 03:28 +1100 |
| Message-ID | <5447db41$0$13000$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #79432 |
Ned Batchelder wrote:
> On 10/22/14 5:05 AM, buscacio@gmail.com wrote:
>> without not:
>> j = [j+1, 3][j>=10]
>> with not:
>> j = [3, j+1][not (j>=10)]
>>
>
> Why on earth would you recommend this outdated hack, when there's a true
> conditional operator?
>
> j = 3 if j >= 10 else j+1
I think that's a bit harsh. Especially since this appears to have been
Buscacio's first post here. Hopefully not his(?) last post!
The old (b, a)[condition] idiom is not outdated for anyone supporting Python
2.4, and I wouldn't call it a hack. Indexing into a sequence with a bool is
basic to Python's semantics: True and False are ints equal to 1 and 0
respectively. It's also a technique easily extensible to more than two
values:
'01TX'[n % 4]
is in my opinion more readable than:
i = n % 4
'0' if i == 0 else '1' if i == 1 else 'T' if i == 3 else 'X'
> Of course, many people feel like the conditional operator isn't worth
> the squished-up unreadability, but if someone asks for a conditional
> operator, at least show them one!
The advantage of the `x if cond else y` operator is that it is a
short-cutting operator, it doesn't evaluate either x or y unless needed.
But for many cases that's not important, and in those cases I won't say I
prefer the old (y, x)[cond] idiom, but neither do I dislike it.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-23 03:40 +1100 |
| Message-ID | <mailman.15083.1413996061.18130.python-list@python.org> |
| In reply to | #79455 |
On Thu, Oct 23, 2014 at 3:28 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > It's also a technique easily extensible to more than two > values: > > '01TX'[n % 4] > > is in my opinion more readable than: > > i = n % 4 > '0' if i == 0 else '1' if i == 1 else 'T' if i == 3 else 'X' That's true when it's fundamentally arithmetic. But part of that readability difference is the redundancy in the second one. What if it weren't so redundant? 'Negative' if x < 0 else 'Low' if x < 10 else 'Mid' if x < 20 else 'High' You can't easily turn that into a dict lookup, nor indexing. It's either a chained if/elif tree or nested if/else expressions, which come to the same thing. So I'd say all the techniques have their advantages and disadvantages. The most important thing to note is where they differ in semantics, like the short-circuiting of if/else. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-10-22 18:03 +0000 |
| Message-ID | <hkS1w.463343$J62.79516@fx03.am4> |
| In reply to | #79455 |
On Thu, 23 Oct 2014 03:28:48 +1100, Steven D'Aprano wrote: >> Why on earth would you recommend this outdated hack, when there's a >> true conditional operator? >> >> j = 3 if j >= 10 else j+1 > > I think that's a bit harsh. Especially since this appears to have been > Buscacio's first post here. Hopefully not his(?) last post! > > The old (b, a)[condition] idiom is not outdated for anyone supporting > Python 2.4, and I wouldn't call it a hack. Indexing into a sequence with > a bool is basic to Python's semantics: True and False are ints equal to > 1 and 0 respectively. It's also a technique easily extensible to more > than two values: > > '01TX'[n % 4] > > is in my opinion more readable than: > > i = n % 4 '0' if i == 0 else '1' if i == 1 else 'T' if i == 3 else > 'X' chained ternary operations are evil no mater what style or language they are written in as they rapidly become confusing & unreadable "Readability counts" in my opinion they are better written as nested if statements -- Ambiguity: Telling the truth when you don't mean to.
[toc] | [prev] | [next] | [standalone]
| From | Matthew Ruffalo <mmr15@case.edu> |
|---|---|
| Date | 2014-10-22 14:27 -0400 |
| Message-ID | <mailman.15088.1414002429.18130.python-list@python.org> |
| In reply to | #79455 |
On 10/22/2014 12:40 PM, Chris Angelico wrote: > That's true when it's fundamentally arithmetic. But part of that > readability difference is the redundancy in the second one. What if it > weren't so redundant? > > 'Negative' if x < 0 else 'Low' if x < 10 else 'Mid' if x < 20 else 'High' > > You can't easily turn that into a dict lookup, nor indexing. It's > either a chained if/elif tree or nested if/else expressions, which > come to the same thing. No, you can't turn that into a dict lookup, but this is one of the canonical use cases for the bisect module: >>> from bisect import bisect >>> breakpoints = [0, 10, 20] >>> labels = ['Negative', 'Low', 'Mid', 'High'] >>> values = [-5, 5, 15, 25] >>> [labels[bisect(breakpoints, value)] for value in values] ['Negative', 'Low', 'Mid', 'High'] It's also worth noting that using bisect is O(log(n)) instead of O(n), but if you're going to hit a point where the asymptotic behavior matters I'm sure you will have long since abandoned a manually-written if/elif chain. MMR...
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-23 05:30 +1100 |
| Message-ID | <mailman.15089.1414002611.18130.python-list@python.org> |
| In reply to | #79455 |
On Thu, Oct 23, 2014 at 5:27 AM, Matthew Ruffalo <mmr15@case.edu> wrote: > On 10/22/2014 12:40 PM, Chris Angelico wrote: >> That's true when it's fundamentally arithmetic. But part of that >> readability difference is the redundancy in the second one. What if it >> weren't so redundant? >> >> 'Negative' if x < 0 else 'Low' if x < 10 else 'Mid' if x < 20 else 'High' >> >> You can't easily turn that into a dict lookup, nor indexing. It's >> either a chained if/elif tree or nested if/else expressions, which >> come to the same thing. > No, you can't turn that into a dict lookup, but this is one of the > canonical use cases for the bisect module: > >>>> from bisect import bisect >>>> breakpoints = [0, 10, 20] >>>> labels = ['Negative', 'Low', 'Mid', 'High'] >>>> values = [-5, 5, 15, 25] >>>> [labels[bisect(breakpoints, value)] for value in values] > ['Negative', 'Low', 'Mid', 'High'] > > It's also worth noting that using bisect is O(log(n)) instead of O(n), > but if you're going to hit a point where the asymptotic behavior matters > I'm sure you will have long since abandoned a manually-written if/elif > chain. Indeed. If the performance is making any difference, something's gone wrong. I'm seeing that as not significantly clearer than the chained if/else statement or expression: you need a list of breakpoints and a list of results, which need to be kept in sync. But there's probably no ideal solution to this. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-10-22 16:11 -0400 |
| Message-ID | <mailman.15090.1414008712.18130.python-list@python.org> |
| In reply to | #79455 |
On 10/22/14 12:28 PM, Steven D'Aprano wrote: > Ned Batchelder wrote: > >> On 10/22/14 5:05 AM, buscacio@gmail.com wrote: > >>> without not: >>> j = [j+1, 3][j>=10] >>> with not: >>> j = [3, j+1][not (j>=10)] >>> >> >> Why on earth would you recommend this outdated hack, when there's a true >> conditional operator? >> >> j = 3 if j >= 10 else j+1 > > I think that's a bit harsh. Especially since this appears to have been > Buscacio's first post here. Hopefully not his(?) last post! You are right, it sounds a bit harsh. Sorry. -- Ned Batchelder, http://nedbatchelder.com
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-10-22 12:41 +0100 |
| Message-ID | <mailman.15069.1413978124.18130.python-list@python.org> |
| In reply to | #79419 |
On 22/10/2014 10:27, Chris Angelico wrote: > On Wed, Oct 22, 2014 at 8:16 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >>> without not: >>> j = [j+1, 3][j>=10] >>> with not: >>> j = [3, j+1][not (j>=10)] >>> >> >> The death penalty should be reintroduced into the UK for two crimes, writing >> code like the above and using google groups. > > No no no. Code like that doesn't deserve death, just community > service. I've seen much MUCH worse... where multiple conditional > expressions get combined arithmetically, and then the result used > somewhere... I also may have been guilty of same, myself, though I'm > going to plead the internet's equivalent of the Fifth Amendment to the > US Constitution and not incriminate myself by showing the code... > > ChrisA > Perhaps you're correct. Is there anything worse than looking at a dreadful piece of code that makes no sense at all and knowing that you'd written it six months earlier? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-10-22 15:12 +0000 |
| Message-ID | <EPP1w.622210$Zu6.97671@fx06.am4> |
| In reply to | #79436 |
On Wed, 22 Oct 2014 12:41:49 +0100, Mark Lawrence wrote: > On 22/10/2014 10:27, Chris Angelico wrote: >> On Wed, Oct 22, 2014 at 8:16 PM, Mark Lawrence >> <breamoreboy@yahoo.co.uk> wrote: >>>> without not: >>>> j = [j+1, 3][j>=10] >>>> with not: >>>> j = [3, j+1][not (j>=10)] >>>> >>>> >>> The death penalty should be reintroduced into the UK for two crimes, >>> writing code like the above and using google groups. >> >> No no no. Code like that doesn't deserve death, just community service. >> I've seen much MUCH worse... where multiple conditional expressions get >> combined arithmetically, and then the result used somewhere... I also >> may have been guilty of same, myself, though I'm going to plead the >> internet's equivalent of the Fifth Amendment to the US Constitution and >> not incriminate myself by showing the code... >> >> ChrisA >> >> > Perhaps you're correct. Is there anything worse than looking at a > dreadful piece of code that makes no sense at all and knowing that you'd > written it six months earlier? looking a a dreadful piece of unreadable & unfathomable code & knowing you wrote it only Yesterday ;-) -- Chamberlain's Laws: (1) The big guys always win. (2) Everything tastes more or less like chicken.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-23 02:18 +1100 |
| Message-ID | <mailman.15077.1413991131.18130.python-list@python.org> |
| In reply to | #79446 |
On Thu, Oct 23, 2014 at 2:12 AM, alister <alister.nospam.ware@ntlworld.com> wrote: >> Perhaps you're correct. Is there anything worse than looking at a >> dreadful piece of code that makes no sense at all and knowing that you'd >> written it six months earlier? > > looking a a dreadful piece of unreadable & unfathomable code & knowing > you wrote it only Yesterday ;-) Sounds like you have some experience with Perl... ChrisA
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-10-22 15:24 +0000 |
| Message-ID | <Z_P1w.622211$Zu6.537403@fx06.am4> |
| In reply to | #79447 |
On Thu, 23 Oct 2014 02:18:42 +1100, Chris Angelico wrote: > On Thu, Oct 23, 2014 at 2:12 AM, alister > <alister.nospam.ware@ntlworld.com> wrote: >>> Perhaps you're correct. Is there anything worse than looking at a >>> dreadful piece of code that makes no sense at all and knowing that >>> you'd written it six months earlier? >> >> looking a a dreadful piece of unreadable & unfathomable code & knowing >> you wrote it only Yesterday ;-) > > Sounds like you have some experience with Perl... > > ChrisA Never met the girl I deny everything. Actual no experience in perl & my professional programming experience is limited to 8 bit assembler (68XX & 8051) 30 years ago. c & c++ were too low level for my liking so I started looking at python for fun on my Linux box's and was blown away by its smoothness. -- On the other hand, life can be an endless parade of TRANSSEXUAL QUILTING BEES aboard a cruise ship to DISNEYWORLD if only we let it!!
[toc] | [prev] | [next] | [standalone]
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2014-10-22 09:09 -0700 |
| Message-ID | <mailman.15080.1413994178.18130.python-list@python.org> |
| In reply to | #79419 |
On Wed, Oct 22, 2014 at 2:05 AM, <buscacio@gmail.com> wrote: > Em quarta-feira, 22 de outubro de 2014 06h29min55s UTC-2, ast escreveu: >> Hello >> >> >> >> Is there in Python something like: >> >> >> >> j = (j >= 10) ? 3 : j+1; >> >> >> >> as in C language ? >> >> >> >> thx > > without not: > j = [j+1, 3][j>=10] > with not: > j = [3, j+1][not (j>=10)] This is not very readable, and eagerly evaluates the two list values. A proper ternary operator will not evaluate the unused candidate-result. This matters a little for performance, but matters more if one or both of the candidate results have side-effects. It's better to use "j = 3 if j >= 10 else j + 1". What you've suggested here was one of the ternary operator workarounds before a true ternary operator was introduced in 2.5. I don't use Python's ternary operator much though - I tend to find if+else more clear, and it shows up well in a debugger. The ternary operator tends to lead to long one-liners (especially if nested!), which are usually best avoided. I'd even go so far as to say that most uses of a ternary operator probably should be a small function. It's slower, but it lends itself to DRY and abstraction better.
[toc] | [prev] | [next] | [standalone]
| From | Tobiah <toby@tobiah.org> |
|---|---|
| Date | 2014-10-24 10:23 -0700 |
| Message-ID | <iWv2w.494585$Y4.165548@fx24.iad> |
| In reply to | #79415 |
On 10/22/2014 01:29 AM, ast wrote:
> Hello
>
> Is there in Python something like:
>
> j = (j >= 10) ? 3 : j+1;
>
> as in C language ?
>
> thx
Out of all of the replies, I don't think anyone
actually offered the answer:
a if condition else b
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-10-25 04:27 +1100 |
| Message-ID | <mailman.15140.1414171648.18130.python-list@python.org> |
| In reply to | #79591 |
On Sat, Oct 25, 2014 at 4:23 AM, Tobiah <toby@tobiah.org> wrote: > Out of all of the replies, I don't think anyone > actually offered the answer: > > > a if condition else b Jean-Michel did, the very first response. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Tobiah <toby@tobiah.org> |
|---|---|
| Date | 2014-10-24 11:31 -0700 |
| Message-ID | <nWw2w.383619$JH1.298730@fx08.iad> |
| In reply to | #79593 |
On 10/24/2014 10:27 AM, Chris Angelico wrote: > On Sat, Oct 25, 2014 at 4:23 AM, Tobiah <toby@tobiah.org> wrote: >> Out of all of the replies, I don't think anyone >> actually offered the answer: >> >> >> a if condition else b > > Jean-Michel did, the very first response. > > ChrisA > I had to search for it. For some reason Thunderbird didn't thread that message with all the others.
[toc] | [prev] | [standalone]
Page 4 of 4 — ← Prev page 1 2 3 [4]
Back to top | Article view | comp.lang.python
csiph-web