I've been trying to write more Pythonic code. I'm sure everyone has their own definition of what Pythonic is, but everyone will agree on things like using list/dict comprehensions where possible and so on and so forth.
I was perusing some code written by some colleagues and found this:
This took me to the basic definition of Python's or keyword and, notably, this clause: "These only evaluate their second argument if needed for their outcome."
Way cool! I would previously have used a construction like
and patted myself on the back for being Pythonic, but there is an even better way to do it.
I must say, though, that I would not use the corresponding and construction because it is not so intuitive to me.
I was perusing some code written by some colleagues and found this:
push_data = {'message': container.message or 'NO COMMIT MESSAGE',
This took me to the basic definition of Python's or keyword and, notably, this clause: "These only evaluate their second argument if needed for their outcome."
Way cool! I would previously have used a construction like
push_data = {'message': container.message if container.message else 'NO COMMIT MESSAGE',
and patted myself on the back for being Pythonic, but there is an even better way to do it.
I must say, though, that I would not use the corresponding and construction because it is not so intuitive to me.
0 and 56
Comments
Post a Comment