Python Forum
[PyQt] Managing variables accross a multiple module GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Managing variables accross a multiple module GUI
#11
i disagree. but then with 40 years of programming experience, i know how to do global and shared variables safely. but if you want to avoid them, by all means avoid them. but you still want some way to communicate between modules. what are you thinking about? messages? events?

i think Denni's point might be if you need to communicate between modules, you have a bad design, somehow. each method, or at least each class should be autonomous as much as you can do it. it should be designed to be something created separate and apart from all the others you happen to be using together in a script that needs them. when designing something big you readily see things you can encapsulate as a function or class. but thinking about it in the context of that big project can result in components that are not autonomously designed. so you need to have another look at your modules and why there is a need to communicate between them. if you can make them autonomous, you should, as that will make each one cleaner.

global and shared variables do exist for a reason. they can be design shortcuts. sometimes the goal is to get something working soon without the delays of good design. but, more experience doing good design lets you do them quicker to the point for many projects the time is the same and/or you have more reusable (because it was well designed) code you can just drop in.

can you describe a couple of modules that need to communicate and let us see how you are doing the design?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#12
Okay @Skaperen then we can agree to disagree on global variables but I am going to guess my many years of experience has been different than yours. Mine have had to work on many ugly post-production projects as well as ground floor development -- I have found that globals (while in old school were necessary sometimes) are and always have been a very dangerous tool to use as such even in old school it had been determined (by many others besides myself) that they ought only be used when absolutely necessary (aka next to never used). Of course this did not stop the lazy minded (or in-experienced) programmers from utilizing them which of course made debugging certain issues a proverbial nightmare. Now in more current coding languages and methodologies the use of globals and semi-globals has become even less of a necessity and as such now mostly only exist because someone was either old school lazy or did not know how to use the language properly.

I grant you there might be cause to use them under extremely rare circumstances (and only if definitely needed) but then they ought to be extremely well documented as well (which rarely happens).

And you pretty much got what I was saying otherwise.

Also I concur @Skaperen drops us your modules and/or concepts and let us help you redesign them such that they are structured cleaner and you can see how such a thing works and then use the concept on current and future projects. Which is to say (if I understood Skaperen correctly) we are both willing to lend you a free hand -- grab this opportunity while its available.
Reply
#13
global variables, whether safe or not (not everyone can make them safe), just aren't needed in a good design. but few of us have time to make everything we do be 100% a good design. we take shortcuts and those bad design often end up with global variables and other things like gotos (if using a language with such things). i spent years programming in assembly (on mainframes) and got to gradually learn better design through the experience of so many mistakes. but, newbies don't have that experience, yet. when i say i disagree, it's about me and the possibility to actually do the elements of a bad design, safely. what i end up with, as a design, is probably well described more as a fractal with smaller and smaller pieces of decent design all mushed (wow, the spell checker thinks that's a real word) together.

i'm not so sure about programming objects. the object has a state and needs to be manipulated. but extra objects found in a global reference are the things we do not have in a good design. whenever different objects need to communicate, maybe it is that class is what has the bad design and bad implementation. but if its API is good, that can shield us until we get a better one.

i'd say we disagree about the rarity. i'd say globals are never needed. but that is the ideal if we have enough time to be sure everything is a good design. but, i also say that good design itself is what is rare. the more our experience, the better our designs, in a limited time. unfortunately, the nature of our humanity, and the resulting economy, limit our time way too much. that's when i end up taking shortcuts. i just try to make those up with components of good design as much as i can. that may mean a global object instead of a mess of random global variables. the inexperienced won't get to see that. sometimes, when guiding them, i want to not force them to jump too far off path, this time. it's better to guide them in ways they will actually do, getting them closer to a better design. with experience they will learn a good design and the advantages it brings. just not today.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
In a nutshell; avoid globals, subclass a lot, and split those class between modules to keep your main file lean :)
Reply
#15
nice sum up @Alfalfa -- now if we can get @Oolongtea to chime back in -- assuming we have not scart them off
Reply
#16
looks like @Oolongtea has not visited since last post 5 days ago.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
Hi, sorry for replying this late I had to put the project on hold for the past few months because of work priorities.
Thank you all for your insight. I've been trying to organise my code like @Alfalfa explained; most control variables are contained within the scope of a class, unfortunately I am unclear where and how my data should be "stored" within the software's code for easy access & modification. This is especially true in the case of measurement data which will be acquired/changed/displayed by various classes in various modules in my project.
On the other hand the structure of the original code is still a real mess, which doesn't help to have a clear picture of what is going on Think

@Denni, @Skaperen, if you are still following the thread I'll try to make a dummy version of my program's structure as soon as i have time.
Reply
#18
I have gone no where ;) and I monitor this and another forum fairly regularly -- stopped monitoring stackoverflow all that much due to a heavy presence of cyber-troll-wannabe-programmers on that forum grant you they got good folks there to its just seems the moderators have let the trolls run amok

As for internal data it kind of depends on what that internal data is and how its being used and where it comes from initially. If your program is not too extensive just post it here and I can give it a once over and perhaps help with its design layout. However if it is fairly extensive then either we can tackle it in pieces here or I have python Discord message server where I am helping other programmers with aspects of python3 & pyqt5 if that interests you shoot me a pm or let me know here with a way to contact you.
Reply
#19
As much as I'd like to dump the whole project to r/programminghorror, I can't post too much code in context for confidentiality reasons. I'll try to come up with a dummy version of the code so I can better illustrate the issues I have with the variables. It might take a bit of time to sift through the mess though..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Tkinter] Defining a self made module's function to interact with the main .py variables? Gilush 9 4,269 Jun-08-2020, 09:08 AM
Last Post: Gilush

Forum Jump:

User Panel Messages

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