Python Forum
Is this good style? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Is this good style? (/thread-7525.html)



Is this good style? - league55 - Jan-14-2018

Just want to know whether this is good style:

print("The length of your string is {} characters.".format(
      len(input("Enter a string: "))))
Also, is this a statement or an expression? I always get confused about those two.


RE: Is this good style? - wavic - Jan-14-2018

It is a callback.
However, I don't like it. It's hard to read.

text = input("Enter a string: ")
print("The length of your string is {} characters.".format(len(text)))
Or:

text_len = len(input("Enter a string: "))
print("The length of your string is {} characters.".format(text_len))
Or:

text = input("Enter a string: ")
text_len = len(text)
print("The length of your string is {} characters.".format(text_len))
It's much easier to see what is going on.


RE: Is this good style? - metulburr - Jan-14-2018

i prefer the 3rd option as it explicitly states each line what it is doing.


RE: Is this good style? - Gribouillis - Jan-14-2018

In python 3 it is a function call, therefore an expression, and as such it is also a statement (an expression statement).

I tend not to include calls to input() in complex expressions, but apart from this, it is good python style.