Python Forum
instance of a class in different files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
instance of a class in different files
#1
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!!!")
Reply
#2
## 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()
Reply
#3
Nice!

wooooee Thanks for your help man Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 471 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Access instance of a class Pavel_47 5 2,082 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  Class Instance angus1964 4 2,438 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,973 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Issue referencing new instance from other class nanok66 3 2,214 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Class variable / instance variable ifigazsi 9 4,296 Jul-28-2020, 11:40 AM
Last Post: buran
  Python complains that class instance is not defined colt 3 5,633 Sep-17-2019, 12:32 AM
Last Post: ichabod801
  how to add class instance attributes from list 999masks 2 2,700 Jul-22-2019, 07:59 AM
Last Post: 999masks
  spread same class into separate files in python asheru93 3 5,466 Jun-19-2019, 02:06 PM
Last Post: ichabod801
  Trying to set an instance variable to current value of a class variable ScottDiesing 3 2,766 Feb-15-2019, 03:12 PM
Last Post: ScottDiesing

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020