Whats wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Whats wrong with my code?

Obviously Python first evaluates the default value. How can I solve it? https://code.sololearn.com/cO354B87EZ0A/?ref=app

6th Jul 2022, 12:47 PM
Oma Falk
Oma Falk - avatar
36 Answers
+ 9
Based on this article https://stackoverflow.com/questions/9761396/dict-get-default-arg-evaluated-even-upon-success Here is a short-circuit solution: print(mydict.get(character) or int(character))
6th Jul 2022, 6:01 PM
Tibor Santa
Tibor Santa - avatar
+ 10
Duck typing :) mydict = {"a":1} word = "1a1aa2a3" for character in word: try: print(mydict[character]) except KeyError: print(int(character))
6th Jul 2022, 5:52 PM
Tibor Santa
Tibor Santa - avatar
+ 8
mydict = {"a":1} word = "1a1aa2a3" for character in word: print(mydict.get(character, character)) # (1) print(int(mydict.get(character, character))) # (2) (1) for printing only it does not matter if 1, 2, 3 are strings or ints (2) if a real integer is required, just put int() around of the dict call
6th Jul 2022, 7:32 PM
Lothar
Lothar - avatar
+ 7
Oma, What was the expected output though? (Edit) If all the characters in the string are within hexadecimal digits range (0 ~ 9, a ~ f) I guess we can just set the optional radix parameter for int() to 16 print(mydict.get(character,int(character, 16))) Obviously it will still fail in the case where a character was not in hexadecimal digits range.
6th Jul 2022, 1:38 PM
Ipang
+ 7
In my opinion this is an implementation bug of the "get" function. The second argument should not be evaluated, unless the key is actually missing from the dictionary. I don't know why it is working this way. On the other hand, maybe it's a feature, not a bug 😆
6th Jul 2022, 5:58 PM
Tibor Santa
Tibor Santa - avatar
+ 6
print( int( mydict.get( character,character ) )) edit: #I tried this way, a similar one... also it can be done by if-else and isdigit() function. but may not fit your approach of python oneliners..
6th Jul 2022, 1:43 PM
Jayakrishna 🇮🇳
6th Jul 2022, 2:20 PM
Simon Sauter
Simon Sauter - avatar
+ 4
Lot of points already mentioned, but i think generally, may be because, those are arguments passed to method. First arguments assignment happens then rest of code block of method gets executed. not a bug. But am not much know about pythonic way of approach..
7th Jul 2022, 8:50 AM
Jayakrishna 🇮🇳
+ 4
Korkunç el Gato I found this in the Python specification / documentation https://docs.python.org/3/reference/expressions.html 6.3.4. Calls (...) "All argument expressions are evaluated before the call is attempted." So it is a general language feature, not specific to dict.get(). That's just how it works. If we want to prevent evaluation, we can pass a functional closure or lambda, then execute it when needed.
7th Jul 2022, 7:16 PM
Tibor Santa
Tibor Santa - avatar
+ 4
#Oma Falk dict crying like why me,when no use... going off (edited code).. text="abcdefghijk" for c in text: print(text[:text.find(c)+1])
7th Jul 2022, 8:23 PM
Jayakrishna 🇮🇳
+ 3
Tibor Santa 😍💯
6th Jul 2022, 6:03 PM
Oma Falk
Oma Falk - avatar
+ 3
Tibor Santa omg. Thaaank you. (I gathered all you people's codes yesterday in a code myself for studying purposes, cause I was unable to understand why the get def arg got evaluated, I'd assumed I'd gotten it wrong in the first place. Thanks everyone for the codes too)
7th Jul 2022, 1:56 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
Tibor Santa It is not a bug in 'get' function, I am still not fully sure about how this works but I think, 'int' function is getting called first and then passed to the get method. See this in two steps: for char in word: a = int(char) mydict.get(char, a) This means arguments get evaluated first before they are sent to the function.
7th Jul 2022, 3:52 AM
Sandeep
Sandeep - avatar
+ 3
Sandeep Well, I think for Python to do eager evaluation for the def arg when there's actually a key is totally counterintuitive. If the dict has a key with a value, evaluating the def arg and doing nothing with it is an extra step. (unless I'm getting this all wrong) It totally makes sense they call this not Pythonic, like T Santa did here too. I was like "oh, so it actually evaluates this", because the error message indicated that, but then I was like "no it cannot be because all these people would've said that", turns out it was, and its unpythonicness is exactly why they didn't readily say that but investigated it in the first place. In retrospect this sort of thing is a pitfall a beginner cannot even know is coming. I just saw some search result where it said Python does mostly eager evaluation. I think this type of info is more important than what courses offer. (What happens under the hood, when)
7th Jul 2022, 5:34 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
Tibor Santa Oh but then doesn't that singlehandedly answer this whole discussion, too? It's totally expected, then. And that's what Sandeep meant all along. Thank you. May I ask you what you think about Sandeep's question, then? Should the way funcs work be changed or is this a matter of knowledge and skill, where you can diminish the number of steps and balance that with storage not just by creativity but by sheer knowledge of how things work?(I added to the question a bit if that's OK)
7th Jul 2022, 7:26 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
The way Pyhton works, is a given for the majority of developers. The language is changing over time, but any change like this, would cause serious issues with backwards compatibility, so it is very unlikely to happen. If there is ever going to be a Python v4 and the core developers decide that such change makes sense and has enough support, theoretically it can happen. But at this point, Python is so popular and prevalent, that this seems impossible to me. So we have to take it with all the annoyances, and adapt our code so it follows the rules :) Such seemingly small annoyances are called "gotcha"s, for advanced pythonists it is worthwhile to study them, so we don't fall into unexpected traps. Python seems to have quite a lot of these which we uncover when we dig deep, not really apparent or disturbing for beginners, but can be annoying for professionals, especially who are used to other languages that work differently.
7th Jul 2022, 7:40 PM
Tibor Santa
Tibor Santa - avatar
+ 3
Tibor Santa Thank you. So theoretically, it's preferable but in reality not feasible for mainly maintaining backwards compatibility and popularity. BTW, apologies for missing how you said from the start that it was the arg itself wrt the whole function body that got eagerly evaluated. I instead thought it wrt the other argument for key, as I had read Python does args from left to right, and I somehow thought using the second as *arg or **kwarg could do it if the method allowed it.
7th Jul 2022, 7:52 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
No worries. The reason was not clear to me initially either, this little research taught me something new as well ;)
7th Jul 2022, 8:01 PM
Tibor Santa
Tibor Santa - avatar
+ 3
7th Jul 2022, 8:08 PM
Oma Falk
Oma Falk - avatar
+ 3
thank y all :] for the amazing insights This shows there are many things to learn when it comes to python and programming 🙌
8th Jul 2022, 3:28 AM
Sandeep
Sandeep - avatar