Python Forum
Using variable from a class in another .py script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using variable from a class in another .py script
#1
Hi, I am trying to get a variable that I set in a class into another .py file. A snippet of the code below:
class Ui_MainWindow(object): #this is the class of the first file, there is more stuff in here on the actual file
    def payment(self):
        self.testvar = "test"
I am trying to get the variable testvar in another python file. I am not good with classes so I am not sure if this is possible.
Thanks in advance.
Reply
#2
You can import the class into a python file like this from [Name of Python File] import [Name Of Class] as [Variable Nickname for class] if they're in the same directory or the one being imported is in the Python Library directory thing. Then you can use the class as if it were in that Python file.
Reply
#3
(Mar-25-2020, 07:09 AM)SheeppOSU Wrote: You can import the class into a python file like this from [Name of Python File] import [Name Of Class] as [Variable Nickname for class] if they're in the same directory or the one being imported is in the Python Library directory thing. Then you can use the class as if it were in that Python file.

Hi, I have tried the follwing:
from application import Ui_MainWindow as mw

print(mw.payment.testvar)
and

from application import Ui_MainWindow as mw

print (mw.payment(testvar))
but it says for the first one AttributeError: 'function' object has no attribute 'testvar' and for the second one NameError: name 'testvar' is not defined am I doing it wrong?
Reply
#4
What you are importing is more a model of the class. Think of it as blueprints, you can't actually interact with it. You have to make something to interact with from the blueprints.
from application import Ui_MainWindow as mw
 
newClass = mw("[Put any initiative parameters in here]") #Makes the class from the "Blueprints"
Also, payment is simply a function that defines the variable, so instead do this
newClass.payment() #Execute function
print(newClass.testvar)#Print the variable
Reply
#5
Ok thanks for the help, I understand now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with threading and reading variable from a different script Lembas 14 3,017 Apr-26-2023, 11:21 PM
Last Post: Lembas
  Variable Types in a class nafshar 9 2,450 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,881 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
Question How can I import a variable from another script without executing it ThomasFab 12 7,745 May-06-2022, 03:21 PM
Last Post: bowlofred
  Calling a base class variable from an inherited class CompleteNewb 3 1,676 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,974 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Class variable / instance variable ifigazsi 9 4,300 Jul-28-2020, 11:40 AM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,264 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  Limiting valid values for a variable in a class WJSwan 5 3,600 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  Use dynamic variable from parallel running python script Sicksym 0 1,840 May-15-2020, 02:52 PM
Last Post: Sicksym

Forum Jump:

User Panel Messages

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