Python Forum
code style - one line subclause on same line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code style - one line subclause on same line
#1
i used to use this style
   if value is True: count += 1
but i have changed to this style
  if value in True:
        count += 1
but now i am reconsidering.  i think that in some cases the one line style can be easier to understand, especially if multiple related clauses are lined up vertically, such as
    if ch in ('t','T'): return True
    if ch in ('f','F'): return False
    else:               return None
so i would not be going back all the way but just part way, just doing it in cases that seem to me to add to readability.  even though this example is simple to understand, IMHO, it is quicker to read this way.

comments?
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
The style that you use is the one I use myself (middle one)
I believe it is more pleasing to the eye.
I often miss code that is written on the same line,
perhaps it's due to my being blind in my right eye.

Even if I wasn't, I wouldn't do it this way, I had already trained
myself when writing 'C'.
Reply
#3
I use the traditional way

if condition:
    count += 1
My mind expects to see this 'if' construction. One line if statement... if condition: count += 1 is harder for me to read it. It should not be like that but it is.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
For me this is just ugly,and goes under Definitely not:  PEP-8.org.
if ch in ('t','T'): return True
if ch in ('f','F'): return False
else:               return None
Reply
#5
to me, it's not ugly and helps guide me to what the logic is, showing, by structure, that 2 distinct tests give 2 distinct actions.  other cases of this kind of code structure don't really happen much in Python (as they do in C) since a dictionary easily simplifies them much better.  i don't expect to use this anywhere near as much in Python compared to C.  i still make things in C.  but, now a lot less.  Python has taken over Pike except in cases where i need Pike's speed (very few).
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
Quote:to me, it's not ugly and helps guide me to what the logic is
And you should do what you like, but I would expect that you will get a lot of comments on the 'odd' style
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb traceback and multi-line statement Skaperen 0 834 Jan-31-2023, 12:23 AM
Last Post: Skaperen
Lightbulb Open-source ML-based line completion IDE plugin Aekalix 0 1,169 Jun-08-2022, 02:28 PM
Last Post: Aekalix
  a form of coded logic in a comand line Skaperen 10 2,881 Mar-02-2022, 08:43 PM
Last Post: Skaperen
  review of command line parsers Skaperen 2 2,062 Mar-11-2021, 07:39 PM
Last Post: Skaperen
  command line options Skaperen 5 2,667 Aug-14-2020, 08:48 AM
Last Post: DeaD_EyE
  opening python from the command line takes a long time to load? abdulkaderanwar 4 2,991 Jun-22-2020, 03:42 AM
Last Post: abdulkaderanwar
  f-string in command line arguments Skaperen 0 1,577 May-05-2020, 11:49 PM
Last Post: Skaperen
  my own command line option parser Skaperen 0 1,664 Mar-27-2020, 04:14 AM
Last Post: Skaperen
  abusing comprehensions for one line loops Skaperen 6 2,525 Dec-16-2019, 11:22 PM
Last Post: Skaperen
  common form of multi-line text Skaperen 3 2,411 Nov-29-2019, 01:38 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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