[Tutor] Calling class from another class

Steven D'Aprano steve at pearwood.info
Wed May 23 21:27:54 EDT 2018

On Tue, May 22, 2018 at 03:46:50PM +0530, aishwarya selvaraj wrote:
>  Dear all,
>  I have created 2 classes in 2 separate files.

If you have learned the bad habit from Java of putting every class in a 
separate file, you should unlearn that habit for your Python code. There 
is no need to put every class in a separate file, and it usually leads 
to complicated dependencies and circular imports.

> File 1 named atcore_py.pyx

"pyx" file extension? Surely not?

> with class andorCameraSDK3, and file 2 with name  AndorCameraGUI making use
> of TKinter.
> I was able to import  andorCameraSDK3 into  AndorCameraGUI, but I was not
> able to do the other way around, as I need to call the function Plot() in
> file 2 inside function acquireimage() in file 1.

There are ways to safely handle circular dependencies in Python:

   file 1 needs to load file 2;

   but file 2 needs to load file 1

but they are annoying, fragile and best left for people with experience, 
not for beginners. The better solution is to put the two classes in the 
one file, and that way there is no import dependency.

> I'm attaching both the files along with this email. It would be great if
> someone could help me out here. Thanks in advance

Please read this:


Thank you!


More information about the Tutor mailing list