I was wondering if anyone helps me in the following case..?
There're two prgs A.py and B.py
A.py has two classes
class x
class y
and class y has some print statements.

I want to import 'class x' to B.py and using the following statement in B.py
from A.py import x

Now, When I execute B.py , it is printing the statements from class y. How does it happen..? Is I am doing anything wrong..? Please help me..

Dani AI

Generated

A short, practical explanation tied to this thread: saw output from y when importing x. That happens because importing a module runs the module body top-to-bottom. Any print statements, expressions, or object creation at module level (including code in a class suite that is not inside a method) execute as the module is loaded. ’s correction helped identify the immediate mistake, but the underlying rule about import-time execution is worth understanding to avoid similar surprises.

Key point: class bodies and top-level statements run at import time. A print placed directly inside a class suite or at the module top will run whether or not that class is instantiated later. The usual way to keep test/demo code from running on import is to put it under the standard main-guard:

if __name__ == "__main__":
    # demo or test code here — won't run when this file is imported as a module

Quick checklist for troubleshooting and good practice:

  • Search the module for prints or function calls at top level or in class suites; move them into methods or under the __name__ == "__main__" guard.
  • Avoid creating instances at module level; prefer factory calls inside functions.
  • Use the logging module for library output instead of bare prints, so behavior can be controlled by the importing application.
  • Be aware of circular imports: they can cause partial module initialization and unexpected side effects.

Further reading in the official docs: Executing modules as scripts and Class definitions.

Recommended Answers

All 3 Replies

Hi,

Could you show us just a schematic of the code at least?

BTW, it's "from A import x" (A is a module, not a file).

I hope the print calls are inside some method of the class x, which you call x.method(), otherwise I don't see how they could get printed ...

you should have :

#file A.py

class x:
    def __init__(self):
        pass #or whatever you need to init

    def x_print(self):
         print 'prin in x'

class y:
    def __init__(self):
        pass #or whatever you need to init

    def y_print(self):
         print 'prin in y'



#file B.py

from A import x

x_inst = x()

x_inst.x_print()   #will never print y's statement

So could you add some details please?

Awesome, Thanks for the code. It's working.

I am new to object oriented language and also for python. Did a mistake when calling the method from the 'x' class. Thanks again.

No problem, being new is good and asking questions is better, as long as you learn from the mistakes...

Good Luck !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.