0

I'm trying to return a default value of r if r does not exist as a key in the hm dictionary. so casting as an int should only occur when r does not exist as a key. yet, it looks like casting is attempted unconditionally. how do I fix the code to represent what I'm trying to do? I could use a ternary but I thought the whole point of the default value field of the dictionary is to be used the way I'm using it below.

hm = {'A': 14, 'K': 13}
r = [hm.get(r, int(r)) for r, _ in ['AC', '3D', 'KH', '4S']]
reactor
  • 1,722
  • 1
  • 14
  • 34
  • 1
    Function arguments are all evaluated before a function is called. So if you pass `int(r)` as a function argument, it will be evaluated before the function is called. Python doesn't know if it's "needed" or not. – Tom Karzes Jan 22 '23 at 18:06

0 Answers0