Hello folks! I'm quite a bit puzzled regarding Python packages, what __init__.py should look like etc.

Let's say I have this structure:

Code:

mypackage/
    __init__.py
    folder1/
        __init__.py
        classA.py
        classB.py
    folder2/
        __init__.py
        classC.py


In each of the class files, there is a class defined, and they might all refer to each other. So class A might use class C and class C might use class A. Thus, we need to import them. However, doing so at top-level gives me a circular import exception, which does make sense. My thoughts are to do magic in the 3 __init__.py files, but I've honestly no idea how.

Can someone give me clues what the contents of each __init__.py file should look like, and how I should import class C from class A (for example)?

Also, one should be able to import every class/function in the package via from mypackage import * (i.e. make everything available to the global scope without having to use the mypackage. prefix. How would I go with that? Thanks in advance Smile
Circular dependencies are usually a result of needing better design. Can you potentially fix the circular dependency issue first?

Also, the python language recommends using just "import x" rather than using "from x import etc".
MateoConLechuga wrote:
Circular dependencies are usually a result of needing better design. Can you potentially fix the circular dependency issue first?


Unfortunately not. These classes can be hundreds to thousands of lines, so placing them in 1 file won't make it any better.

Also, if more explanation is needed: these classes are custom "types", and initializing a class checks the type of the input, which logically can be another of my custom types. Also, builtin functions like def __invert__(self) can change the custom type as well. That's why they need to import each other.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement