OpenDataServices / developer-docs

Documentation about how we get our work done. For internal use, but happy to share
Other
1 stars 0 forks source link

What is the our Python import preference? #64

Open odscjames opened 6 years ago

odscjames commented 6 years ago

A)

import os
...
x = os.path.join(...)

B)

from os.path import join
...
x = join(...)

Discuss!

I think I prefer A? It makes it clearer when reading code what is being called, without having to check the imports, which might be a long way away from the code.

I don't think PEP8 covers this? (It says prefer on separate lines, prefer absolute imports where possible, and no wild card (*) imports).

BibianaC commented 6 years ago

In the case of os, I prefer A.

If it would be something else like datetime I would prefer: from datetime import datetime (or any other)

instead of import datetime and then having to do datetime.datetime

odscjames commented 6 years ago

Is there some rules you have for when you prefer A or B?

I've just found issues with circular imports when using type B and now I think I need to go and read up more on how Python imports work, as I'm clearly missing some details!

BibianaC commented 6 years ago

This gist might help https://gist.github.com/datagrok/40bf84d5870c41a77dc6 In terms of style you can also read Pep8 https://www.python.org/dev/peps/pep-0008/#imports

odscjames commented 6 years ago

That's an interesting read, thanks!

As far as I can tell, PEP8 doesn't cover my original question. There is a section on "When importing a class from a class-containing module, ...." that touches on this, but it doesn't actually express a preference!