Python Forum
Passing Variables up and down through classes
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing Variables up and down through classes
#1
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.
Reply
#2
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 
Recommended Tutorials:
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organizing several similar classes with overlapping variables 6hearts 7 1,376 May-07-2023, 02:00 PM
Last Post: 6hearts
  Passing Variables between files. victorTJ 3 2,245 Oct-17-2020, 01:45 AM
Last Post: snippsat
  Python 2.7 passing variables from functions zetto33 1 1,775 Mar-19-2020, 07:27 PM
Last Post: Larz60+
  help with threading module and passing global variables ricardons 1 7,837 Feb-21-2019, 12:48 PM
Last Post: stullis
  I can't use file __init__ to store shared variables and classes in the package AlekseyPython 2 3,327 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  How can classes access each other Functions and Variables at the same time PythonOK 4 3,038 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  Class Modules, and Passing Variables: Seeking Advice Robo_Pi 21 10,249 Mar-02-2018, 05:22 PM
Last Post: snippsat
  Using classes? Can I just use classes to structure code? muteboy 5 5,033 Nov-01-2017, 04:20 PM
Last Post: metulburr
  global variables, classes, imports Skaperen 16 10,597 Mar-02-2017, 11:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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