Python Forum
code style - one line subclause on same line - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: code style - one line subclause on same line (/thread-6551.html)



code style - one line subclause on same line - Skaperen - Nov-28-2017

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?


RE: code style - one line subclause on same line - Larz60+ - Nov-28-2017

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'.


RE: code style - one line subclause on same line - wavic - Nov-28-2017

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.


RE: code style - one line subclause on same line - snippsat - Nov-28-2017

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



RE: code style - one line subclause on same line - Skaperen - Nov-29-2017

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).


RE: code style - one line subclause on same line - Larz60+ - Nov-29-2017

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