👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"m trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so:
main.py
file:
import mymixin.py
class Main(object, MyMixin):
def func1(self, xxx):
...
mymixin.py
file:
class MyMixin(object):
def func2(self: Main, xxx): # <--- note the type hint
...
Now, while this works just fine, the type hint in MyMixin.func2
of course can"t work. I can"t import main.py
, because I"d get a cyclic import and without the hint, my editor (PyCharm) can"t tell what self
is.
I"m using Python 3.4, willing to move to 3.5 if a solution is available there.
Is there any way I can split my class into two files and keep all the "connections" so that my IDE still offers me auto completion & all the other goodies that come from it knowing the types?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Python type hinting without cyclic imports, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Python type hinting without cyclic imports
- Deutsch Python type hinting without cyclic imports
- Français Python type hinting without cyclic imports
- Español Python type hinting without cyclic imports
- Türk Python type hinting without cyclic imports
- Русский Python type hinting without cyclic imports
- Português Python type hinting without cyclic imports
- Polski Python type hinting without cyclic imports
- Nederlandse Python type hinting without cyclic imports
- 中文 Python type hinting without cyclic imports
- 한국어 Python type hinting without cyclic imports
- 日本語 Python type hinting without cyclic imports
- हिन्दी Python type hinting without cyclic imports
Berlin | 2023-03-22
Simply put and clear. Thank you for sharing. Python type hinting without cyclic imports and other issues with types Python module was always my weak point 😁. I just hope that will not emerge anymore
San Francisco | 2023-03-22
re Python module is always a bit confusing 😭 Python type hinting without cyclic imports is not the only problem I encountered. I am just not quite sure it is the best method
Berlin | 2023-03-22
Thanks for explaining! I was stuck with Python type hinting without cyclic imports for some hours, finally got it done 🤗. Checked yesterday, it works!