Python Forum

Full Version: instance of a class in different files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All!

I'm new to Python Smile

Trying to understand how classes work around, i'm trying to use a method that is in another class from a different file.

Note: I'm using Pycharm and i created a simple project, both files are in the same folder.

Any ideas on how to access classB methods??

Eg:

File1.py
from File2 import classB

classA:
my_object = classB()
my_object.showGreeting()


File2.py

classB:
def showGreeting(self):
print("Welcome!!!")
## File1.py

import File2

class A:
    def __init__(self):
        ## note that ClassB was incorrectly named in your code
        ## there is a space in the name and it is not CamelCase
        class_b_instance=File2.ClassB()
        class_b_instance.showGreeting()
Nice!

wooooee Thanks for your help man Dance