0

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

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by tzushky
1

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?

Edited by tzushky: n/a

0

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.

1

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

Good Luck !

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.