...This sort of code might be better as a single expression. For example:LOL. And I thought I was the one with a (self-confessed) tendency to
user = (
request.GET["user"]
.decode("utf-8")
.strip()
.lower()
)
user = orm.user.get(name=user)
write too slick, dense, smart-alec code. 😁
Indeed, I was itching to shorten it (starting with the low-hanging
fruit: user = user.strip().lower() ).
Seriously though: this kind of condensation can come unstuck when any of
the steps need to be made more complicated.
Peter's actual code feels more Pythonic to me. (It's even 2 lines
shorter! 😎)
I sometimes have a chain of transformations (e.g. first decode it, then strip extra spaces, then normalize spelling, then look it up in a
database and replace it with the record, ...). Technically, of course
all these intermediate objects are different, and I could make that
explicit by using different variable names:
user_param = request.GET["user"]
user_decoded = str(user_param, encoding="utf-8")
user_stripped = user_decoded.strip()
user_normalized = user_stripped.lower()
user_object = orm.user.get(name=user_normalized)
But I find it easier to read if I just reuse the same variable name:
user = request.GET["user"]
user = str(user, encoding="utf-8")
user = user.strip()
user = user.lower()
user = orm.user.get(name=user)
Each instance only has a livetime of a single line (or maybe two or
three lines if I have to combine variables), so there's little risk of confusion, and reusing the variable name makes it very clear that all
those intermediate results are gone and won't be used again.
However, (continuing @Peter's theme) such confuses things when something
goes wrong - was the error in the input() or in the float()?
- particularly for 'beginners'
- and yes, we can expand the above discussion to talk about
error-handling, and repetition until satisfactory data is input by the
user or (?frustration leads to) EOD...
On Wed, 24 May 2023 at 10:12, dn via Python-list <python-list@python.org> wrote:
However, (continuing @Peter's theme) such confuses things when something
goes wrong - was the error in the input() or in the float()?
- particularly for 'beginners'
- and yes, we can expand the above discussion to talk about
error-handling, and repetition until satisfactory data is input by the
user or (?frustration leads to) EOD...
A fair consideration! Fortunately, Python has you covered.
$ cat asin.py
import math
print(
math.asin(
float(
input("Enter a small number: ")
)
)
)
$ python3 asin.py
Enter a small number: 1
1.5707963267948966
$ python3 asin.py
Enter a small number: 4
Traceback (most recent call last):
File "/home/rosuav/tmp/asin.py", line 4, in <module>
math.asin(
ValueError: math domain error
$ python3 asin.py
Enter a small number: spam
Traceback (most recent call last):
File "/home/rosuav/tmp/asin.py", line 5, in <module>
float(
ValueError: could not convert string to float: 'spam'
Note that the line numbers correctly show the true cause of the
problem, despite both of them being ValueErrors. So if you have to
debug this sort of thing, make sure the key parts are on separate
lines (even if they're all one expression, as in this example), and
then the tracebacks should tell you what you need to know.
On Wed, 24 May 2023 at 10:12, dn via Python-list <python-list@python.org>wrote:
However, (continuing @Peter's theme) such confuses things when something
goes wrong - was the error in the input() or in the float()?
- particularly for 'beginners'
- and yes, we can expand the above discussion to talk about
error-handling, and repetition until satisfactory data is input by the
user or (?frustration leads to) EOD...
A fair consideration! Fortunately, Python has you covered.
$ cat asin.py
import math
print(
math.asin(
float(
input("Enter a small number: ")
)
)
)
$ python3 asin.py
Enter a small number: 1
1.5707963267948966
$ python3 asin.py
Enter a small number: 4
Traceback (most recent call last):
File "/home/rosuav/tmp/asin.py", line 4, in <module>
math.asin(
ValueError: math domain error
$ python3 asin.py
Enter a small number: spam
Traceback (most recent call last):
File "/home/rosuav/tmp/asin.py", line 5, in <module>
float(
ValueError: could not convert string to float: 'spam'
Note that the line numbers correctly show the true cause of the
problem, despite both of them being ValueErrors. So if you have to
debug this sort of thing, make sure the key parts are on separate
lines (even if they're all one expression, as in this example), and
then the tracebacks should tell you what you need to know.
Perhaps more psychology rather than coding?
Note that the line numbers correctly show the true cause of the
problem, despite both of them being ValueErrors. So if you have to
debug this sort of thing, make sure the key parts are on separate
lines (even if they're all one expression, as in this example), and
then the tracebacks should tell you what you need to know.
Yes, an excellent example to show newcomers to make use of 'the
information *provided*' - take a deep breath and read through it all, >picking-out the important information...
However, returning to "condense this into a single line", the
frequently-seen coding is (in my experience, at least):
quantity = float( input( "How many would you like? " ) )
which would not produce the helpful distinction between >line-numbers/function-calls which the above (better-formatted) code
does!
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 75:40:13 |
Calls: | 6,716 |
Calls today: | 4 |
Files: | 12,247 |
Messages: | 5,357,403 |