Python Forum
a preferred choice os the two lines?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a preferred choice os the two lines?
#1
is there a preferred way of coding stuff like this (choose one line of these two):

working_column = self.columns[-1] if len(self.columns) > 0 else [0]
working_column = [0] if len(self.columns) < 1 else self.columns[-1]
or does it just not matter and is left to personal preference or arbitrary chance?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Personally, I tend to put the outcome I expect is more likely first.
Reply
#3
why not just
working_column = self.columns[-1] if self.columns else [0]
Reply
#4
i personally avoid single line conditionals at all
Recommended Tutorials:
Reply
#5
(May-16-2017, 12:15 PM)metulburr Wrote: i personally avoid single line conditionals at all

do you have a reason for that which you believe others should consider?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(May-17-2017, 08:38 AM)Skaperen Wrote: do you have a reason for that which you believe others should consider?

because I also avoid ternary operator - in my opinion it's less readable, but it is personal preference.
Reply
#7
different opinions
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
I actually like ternary operations. It makes it very clear that you're always setting a new value to the variable, which can otherwise get lost along the way once the code is passed around a few people.
Reply
#9
If I have to read it twice I'd prefer a normal conditional
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
i like ternary operators when what i am having the code match ehat i am thinking.  this would include a case where i am assigning a single target with a value or a different value or a case where an expression needs to vary based on a conditional.  this is something i did even before i programmed in C.  way back in my macro assembler days i wrote a set of macros to do structured programming.  i included the capability to use a conditional to establish a value apart from the code that would store it, or use it in some way.  i recognized that logic could have meanings like this and sought to make my code express the meaning as well as it could.  so finding that C and Pike supported ?: and Python had a way (even if it was different) made me even happier with these languages.
Tradition is peer pressure from dead people

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  combobox_callback(choice choice part of openfile name (kind of dependency) janeik 9 1,446 Sep-10-2023, 10:27 PM
Last Post: janeik
  random.choice HELP samuelbachorik 4 2,267 Aug-18-2021, 03:24 PM
Last Post: naughtyCat
  ???: if "{choice}" in self._command BaiYouLing4 3 2,029 Aug-23-2020, 05:40 AM
Last Post: BaiYouLing4
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,834 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Preferred way to slice a list SvetlanaofVodianova 3 2,549 Dec-09-2019, 11:50 PM
Last Post: SvetlanaofVodianova
  Is Event.set() the preferred way to stop a thread? svetlanarosemond 5 3,832 Jul-17-2018, 08:14 AM
Last Post: DeaD_EyE
  preferred?: multiple assignment Skaperen 6 6,109 Dec-19-2016, 06:18 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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