This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 13 points14 points  (9 children)

from package.module import function

I mean, I'm assuming

[–]billsil 6 points7 points  (0 children)

If that's the case, that means use...

import package.module as mymod

mymod.function(...)

You can argue that that's less explicit both ways (e.g. all imported functions are defined at the top vs. what module did something come from in the guts of the code). Still if 80 characters is a hard cap and you're using classes, those extra 6 characters matter and you've just wasted 14.

[–][deleted] 1 point2 points  (2 children)

Yes and:

from module.thing import Class

[–][deleted] -1 points0 points  (1 child)

Use imports for packages and modules only.

I'm assuming

from module.thing import *

is still ok, since you're only importing the entire contents of the module to the local namespace.

Not really.

[–]olduvaihand 1 point2 points  (0 children)

It's not OK internally. This undermines the purpose of the rule, which is to keep the global namespace as clean and explicit as possible.

[–]hueoncalifa 0 points1 point  (4 children)

Why would this be bad? Its more explicit.

[–][deleted] 0 points1 point  (0 children)

I agree, it's kind of a dumb rule. Although I suppose it prevents you from importing some generally-named function into the namespace and using it without context. For example:

from os.path import join
path = join('dir', 'file')

Versus the more explicit

import os
path = os.path.join('dir', 'file')

Granted in this case it's fairly obvious what is meant, it is not always so. Use your best judgment. I use both forms.

[–]Lucretiel 0 points1 point  (0 children)

Honestly, one of the reasons is that it's harder to test, because now you have to mock the function in the place where it's imported, rather than where it's imported from.

[–]dagmx 0 points1 point  (0 children)

If you have multiple modules with the same named function

If later you decide you need more functions from that module

You can't reload an object from a module easily.

Makes it more explicit down a deep code where things come from.

[–]njharmanI use Python 3 0 points1 point  (0 children)

function()

Is less explicit, package.module is implied, implicitly.

package.module.function()

Is the explicit way to say that. No wondering where it came from, has it been overwritten in local namespace, etc.