Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
boolean result of loop
#11
I don't want anything but @scaperen do. On one line. A universal approach. If it can be done, I think it will lose readability.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#12
(Apr-01-2017, 11:20 AM)wavic Wrote: I don't want anything but @scaperen do. On one line. A universal approach.
Actually he didn't say oneliner...
Reply
#13
(Apr-01-2017, 12:07 PM)buran Wrote: he didn't say oneliner

Yeah, he did. Post #3. That's what I was responding to. Perhaps I should have quoted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
hopefully, it can be done in one line.  if not, then two lines would be good.  but if unreadable, then it's not worth it.

when we know some (any) classic logic, then it would be nice to express it succinctly.  IMHO, a loop to look for some condition (or value) and yield (or use) it, falls into this category.  this is at the level of structure.  so, a language having nothing more than structure, could include this.  those with more, should.  with the features python has, it should be easy.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#15
you can do

if 'foo' in d:
    something = d['foo']
else:
    something = False
or you can do

something = d.get('foo',False)
see... 1 statement instead of 4

bonus points: who can show how to do this in 1 statement for a sequence

if x < len(s):
    something = s[x]
else:
    something = False
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#16
(Apr-02-2017, 02:52 AM)Skaperen Wrote: bonus points: who can show how to do this in 1 statement for a sequence

if x < len(s):
    something = s[x]
else:
    something = False

something=s[x] if x < len(s) else False
or
something=[False,s[x]][x < len(s)]
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#17
The second way, isn't it deprecated?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#18
(Apr-02-2017, 11:34 PM)wavic Wrote: The second way, isn't it deprecated?
Deprecated usually means it was recommended at the time :)

It was an ok hack, but the behavior is not the same. When you build the list, its contents get evaluated, whereas with the longer form or the ternary expression it evaluates only what it needs to. This is important if there are side effects (aka EVIL) or even if there is a performance cost associated with one or both values.
Reply
#19
I confess that I've never used this.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#20
I used it once on the old board and everyone got mad at me. ; )
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running A Loop Until You See A Particular Result knight2000 6 31,606 Sep-04-2021, 08:55 AM
Last Post: knight2000
  Noob Alert! Wrong result using loop and if statemnent GJG 7 2,794 Dec-19-2020, 05:18 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020