How can import variable beteen classes in same module - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: How can import variable beteen classes in same module (/thread-26023.html) |
How can import variable beteen classes in same module - johnjh - Apr-18-2020 I need to import this path variable from first class to another classes, this is my code: import openpyxl class Excell(): global path def send_message(self, data): global path print("Path Excel is : '{}'".format(data)) path = data # I need to export this variable 'path' to class First() class First(): global path wb = openpyxl.load_workbook(path) sheet = wb['sheet'] firstCell= sheet["A1"].value print("Cell is :" + firstCell)After run code, I see this message:
RE: How can import variable beteen classes in same module - deanhystad - Apr-19-2020 Use a class variable. You kind of almost have this except you need to leave of the "global". class Excell(): path = "" # This is a class varaible def send_message(self, data): print("Path Excel is : '{}'".format(data)) self.path = data # Assigning value to Excell.path class variable class First(): wb = openpyxl.load_workbook(Excell.path) # Using the class variable outside the class sheet = wb['sheet'] firstCell= sheet["A1"].value print("Cell is :" + firstCell) |