On 11/14/2022 5:36 PM, Barry wrote:
On 14 Nov 2022, at 22:06, Thomas Passin <li...@tompassin.net> wrote:
For parameter passing like your #2, I have packaged them into a dictionary and
passed that around. It was easy enough, and worked well.
I used to use a dict but having been burnt with issues now create a class.
With a class you can add accessor methods that allow you to run debug code as
values are changed. Also you can check that values being set are spelt
correctly, have reasonable values etc.
Barry
Of course, it all depends on how you are going to use the parameters,
whether you plan to mutate some of them in place, etc. Mutating some of
the input parameters can be useful, but also can lead to bugs that are
harder to track down. For simple cases, dicts will probably be fine.
Otherwise, classes or data classes may be better, as you suggest.
The only potential problem is in documenting the key/value pairs the dictionary
is supposed to contain. You had better make sure it's made clear somewhere,
preferable where it is consumed.
For "global" values, sure, put them in a module and import them as needed.
Just make sure that none of your code is going to mutate those values, or you will end up
with mass confusion. I suppose you could make them be properties that have no setters,
if you want to go to that trouble.
On 11/14/2022 12:14 PM, Stephen Tucker wrote:
Hi,
I have two related issues I'd like comments on.
Issue 1 - Global Values
Some years ago, I had a situation where
(a) I could supply low-level functions that carry out tasks,
(b) I needed those functions to communicate with each other, but
(c) I had no access to the module that invoked my functions.
In order to achieve this, I hit on the idea that I also supply a module
that I describe as a "global values" module. This module …
(i) … defines the values that the functions use to communicate with each
other;
(ii) … is the subject of an import statement in each of my functions;
(iii) … initialises its values at the start of each run (since the import
only carries out an actual import once per session);
(iv) … acts as a repository for the values thereafter.
This solution works well.
Given that I am not particularly concerned about efficiency,
(1) Is this a reasonable way to achieve this goal?
(2) Do you know of any better ways?
Issue 2 - Passed Parameters
I am now facing another situation where I am wanting to pass 6 or 7
parameters down through several layers of logic (function A calling
function B calling ... ) and for results to be passed back.
Having had the idea described above, I am considering using it again to
save all the parameter-and-results passing.
I see nothing wrong with doing that, but I may well be missing something!
Comments, please!
Stephen Tucker.
--
https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list