Python Forum
why is user = form.getfirst("user", "").upper() safe?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why is user = form.getfirst("user", "").upper() safe?
#7
No, this is the solution, not the example code for error.
The example code for error is this one:
user = form.getvalue("user").upper()
now, if form.getvalue("user") returns list and you call upper() on it you will get an error.
as explained in the docs, you can test for what is returned. but there is alternative - if instead you use
user = form.getfirst("user", "").upper()
it will return just the first element (single string) or "" and upper() will not raise exception

you can also use form.getlist() in which case you will always get a list. In any case the code is more compact and clean compared to the one if you check what is returned.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: why is user = form.getfirst("user", "").upper() safe? - by buran - Nov-27-2018, 07:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,197 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  run SQL without user intervention python dawid294 0 269 Jan-19-2024, 01:11 PM
Last Post: dawid294
  When is it safe to compare (==) two floats? Radical 4 829 Nov-12-2023, 11:53 AM
Last Post: PyDan
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,138 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Help on the User Interface Afia 1 585 Jul-21-2023, 07:22 PM
Last Post: snippsat
  restrict user input to numerical values MCL169 2 988 Apr-08-2023, 05:40 PM
Last Post: MCL169
  Replace with upper(string) WJSwan 7 1,667 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  user input values into list of lists tauros73 3 1,126 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,165 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,165 Dec-11-2022, 07:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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