[Tutor] reading configuration file
Eur van Andel
eur at fiwihex.nl
Tue Nov 11 11:13:16 EST 2003
On Tue, 11 Nov 2003 13:28:48 +0000, Gonçalo Rodrigues <op73418 at mail.telepac.pt>
wrote:
>>Or do I have to invoke config[...] ?
>>Do the elements of the dictionary become global variables?
>
>You have to use the config dictionary.
>
>>
>>Somehow the purpose of a dictionary eludes me.
>
>There are 2 main problems with your approach.
>
>- You are polluting the global namespace with a lot of variables.
Yes, that is my FORTRAN heritage. Sorry.
>Since they are all related isn't it better to put then all together?
Yes.
>And now you can do other things. For example, write a report based on
>the config dictionary, e.g. something like
>
>>>> DEFAULTDICT = {"CO2_soll": 81}
>>>> lst = ["The config dictionary."]
>>>> for key, value in DEFAULTDICT.iteritems():
>... lst.append("%s = %s" % (key, value))
>...
>>>> print "\n".join(lst)
>The config dictionary.
>CO2_soll = 81
>
>Now try to do that with global variables.
You are right.
>- Global variables are almost always BAD. In your example they were
>BAD because they got tightly coupled with the parse function. A
>function should be like a black box: it receives input via the
>arguments it receives, it gives output via return values. There should
>be no more interaction with its environment. By using global variables
I see that now too. I was used to globals. Sometimes, with not too many globals
it is easier, like in a microcontroller.
>as you did you made the parse function dependent on its environment -
>a recipe for disaster. By avoiding global variables and having a
>dictionary as a return value we have made our function like a black
>box - no side effects on the surrounding environment.
>
>Any more question just holler,
>G. Rodrigues
>
>P.S: In the future send emails *also* to the Tutor list. That way all
>the participants can chime in and, hopefully, learn something.
I replied. I see now that replying doesn't work on the tutor mailing list. Why?
--
Ir. E.E. van Andel, Fine Wire Heat Exchangers, Fiwihex B.V. www.fiwihex.com
Wierdensestraat 74, NL-7604 BK Almelo, The Netherlands eur at fiwihex.nl
phone +31-546-491106 fax +31-546-491107 mobile +31-653-286573
More information about the Tutor
mailing list