4

I currently have:

tmp = myfunc()
mydict[mykey] = tmp
return tmp

..which seems a little too long. In javascript, I could just do:

return (mydict[mykey] = myfunc()) 

Is the above Python code the accepted way to do it, or is there something else?

edit: I'm aware of the possibility of doing:

mydict[mykey] = myfunc()
return mydict[mykey]

..but I wouldn't want to do a key lookup twice. Unless

3
  • 2
    Good Python coding practice is to try to write the most understandable code rather than the shortest. Commented Jul 3, 2012 at 2:20
  • 4
    This example makes it look as though you've written a function that produces side effects and returns a value. That's sometimes frowned upon. When it is necessary, the verbosity that results is probably warranted. Commented Jul 3, 2012 at 2:51
  • @MichaelHoffman would rather say: Good Python coding practice is to try to write the most understandable code and then keep it shortest. Commented Jul 3, 2012 at 4:13

3 Answers 3

12
tmp = mydict[mykey] = myfunc()
return tmp
Sign up to request clarification or add additional context in comments.

2 Comments

why not return (mydict[mykey] = myfunc()) then
@zinking I don't think you can assign a variable that way in python
4

You can do this if you want less lines of code:

mydict[mykey] = myfunc()
return mydict[mykey]

Assignment isn't an expression in Python, though, so you can't do the javascript version.


EDIT: If you know the key is not in the dictionary, you can do this:

return mydict.setdefault(mykey, myfunc())

setdefault is a lookup function that sets the key to the 2nd value if the key is not in the dictionary.


You could also write a helper function:

def set_and_return(d, k, v):
    d[k] = v 
    return v

Then, everywhere else, you can do:

return set_and_return(mydict, mykey, myfunc())

4 Comments

@JakobBowyer: for less lines of code. I personally wouldn't do it; just addressing the OP.
As Jakob said, I would rather avoid doing that twice. edit: Good edit, I'll use setdefault. Thank you!
@Walkerneo - using setdefault is only safe if the key is not in the dictionary. Otherwise, it will never set the new value.
Using setdefault doesn't really convey the intent to someone reading the code.
2

Opinions vary but here is my $.02.

  1. Please do not use default dict. If I'm reading your code, I might not know what you know, that there is no key in the dict. Someone could later update the code and violate that silent assumption and the code will break.
  2. Please do not use the "set_and_return" function. Your code might be clever but it's less readable. More lines, harder to follow and the function lookup costs the same as the dict lookup.
  3. @gnibbler has a nice "fewer lines of code" solution but I wouldn't suggest it and reading a lot of python source I rarely see that syntax (more at the start of a function than anywhere else)

I appreciate how it happens in javascript but for the accepted way to do it: I vote for your original format, easiest to read and understand and no slower or less efficient than any other solution.

tmp = myfunc()
mydict[mykey] = tmp
return tmp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.