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..

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.