svisser / python-improvements

Common improvements for your Python projects
MIT License
26 stars 1 forks source link

as "fd" not "f" #1

Open guettli opened 8 years ago

guettli commented 8 years ago

Hi,

thank you for these hints.

Here is some feedback:

I think it is much better to use "fd" and not "f" if you store a file descriptor :-)

with open(...) as fd:
    ...
svisser commented 8 years ago

Hey there, thanks for your feedback. I'm going to look into this; the Python documentation says it returns a file object which means f is possibly clear enough. I think the object that you get back is a bit higher level than a file descriptor (as used at operating system level) but I could be wrong here.

https://docs.python.org/3/library/functions.html#open

guettli commented 8 years ago

@svisser my concerns are not the technical details. It is "best practice". I see "fd" very often. And I personally think that one letter variables are not clean code. I just wanted to tell you this.

I checked the python source ...

===> find /usr/lib/python2.7/ -name '*.py'|xargs grep 'with open' | sed -nre 's/^.*as (.*):.*$/\1/p'| sort | uniq -c | sort -rn
    113 f
     22 fp
     16 fd

... the result is not what I guessed before. The single character f is very common.