Python Forum

Full Version: Passing Variables up and down through classes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,  I have two problems I would love to have help with.  First up,  My problem is knowing the right Pythonic terminology to use to search for the solution to my problem.   Without knowing the correct terminology I have foundering.    Second up.  I would love some constructive assistance to help me with the actual problem.


I am using Python 3.4 with PySide to create a little application.   One of the components of the application is a Configuration dialog which allows the user to, well, configure the app.

My Application is currently structured with classes that relate to each other in the following way.   There are more but the Configuration <-> guiConfigWindow relationship is the one I am trying to make work right now.

    Main -\
          | Configuration
          |- guiMainWindow -\
                                        |
                                        \--guiConfigWindow

How do I make it so that the Configuration Class can be read and written to by the guiConfigWindow class AND other classes throughout the application.    What is the correct Pythonic term for what I am trying to do?


As you can see,  if I knew the correct terminology I would probably be able to search for the answer already.
One way is to use inheritance.

An example of how sub classes can alter the class variable "count"
class Employee:
    count = 0
    def __init__(self):
        Employee.count += 1
        
class FullTime(Employee):
    pass 

class PartTime(Employee): 
    def add_to(self, num):
        Employee.count += num
        
print(Employee.count)
obj1 = FullTime()
obj2 = FullTime()
obj3 = PartTime()
print(Employee.count)
obj3.add_to(3)
print(Employee.count)
Output:
0 3 6
All classes the super class Employee and the two sub classes FullTime and PartTime share count value. Whereas if you created an instance variable via self.var it would be per object.

so in your case
class Configuration:
    pass 
    
class guiMainWindow(Configuration):
    pass 
    
class guiConfigWindow(guiMainWindow):
    pass 
(May-09-2017, 12:43 AM)metulburr Wrote: [ -> ]One way is to use inheritance.
Thank you,   Your example was very clear I will see how I go.   Also, now i know that Inheritance IS what I am talking about I have a lot of resources I can plough through to get deeper examples.
It's actually quite amazing just how far you can get with Python and development without ever having to use inheritance.