-3

I've been programming in Python and ran into a problem that I think makes my code a lot more messy, and I'm trying to find a workaround. It's a large program, so instead of just dropping it here and making you sort through it I'll give a summary of my problem.

Basically, my program consists of 3 files. The first, main.py, looks like this:

import util
import content

util.myfunc('unique parameters')
content.otherfunc()

The second, content.py, looks like this:

import util

def otherfunction()
  util.myfunc('unique parameters')

What's happening is that I want to use the util module's functions in content and main.

The problem here is that, I have this messy problem where I can call all the stuff from util from content from main.

main.py:

import content
import util

content.util.myfunc()

but I don't want to have to do this because content is not related to util in the sense that it only imports it to use the functions. They do not have a shared purpose. Also, this results in importing util twice. I'm trying to get neater code and would like to find a solution WITHOUT importing util twice. Any ideas?

2
  • 2
    Don't worry about it. util is only evaluated the first time it is imported. Commented Oct 1 at 17:05
  • it is normal think to put import util it in all files which use util in code - so stop bother this. Commented Oct 1 at 17:37

1 Answer 1

2

Do nothing. Your first code sample works fine, even if both modules happen to import the same utility module. It does so happen that content.util.myfunc() is valid, but you're unlikely to use it on your own, and that doesn't make it impossible to also call util.myfunc().

I also would not worry about having multiple import util lines. The import line appears once in each file where you call a util function. That's a clear rule, and it makes it easy to tell what your dependencies are even if you're directly importing a file for a unit test, or an alternate entry point, or something else.

If it really bothers you that it's possible to call content.util, you can use the __all__ special variable to limit the included module to only the specific things you want to export:

import util

def otherfunction() -> None:
  util.myfunc('unique parameters')

__all__ = ["otherfunction"]

With this, if you import content, you will still be able to call content.otherfunction(), but content.util will no longer be defined and you can't invoke the module that way. (If you are extremely strict about API stability, then removing the import util statement from the intermediate module won't change the module API if it was not exported.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! And sorry if my question came off as irritating (I noticed I currently have -4 votes), I just want to work on my convention as I am currently taking a computer science class, and am still getting a sense of what is considered sloppy or not. I just learned apparently global variables are bad(?), so i'm kind of on my toes.
Global variables are in fact bad.

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.