Chris's Wiki :: blog/python/AvoidingGlobals Commentshttps://utcc.utoronto.ca/~cks/space/blog/python/AvoidingGlobals?atomcommentsDWiki2010-11-14T09:51:35ZRecent comments in Chris's Wiki :: blog/python/AvoidingGlobals.From 216.131.118.57 on /blog/python/AvoidingGlobalstag:CSpace:blog/python/AvoidingGlobals:fc00932c0176d0a2f5200ad77c41ebf465cde45cFrom 216.131.118.57<div class="wikitext"><p>`global' irritates me, too. I'd (1) pass in a dictionary, or (2) use default arguments (if the argument type is immutable), or (3) decorate the function with caching (if the argument type is mutable. in this case default argument irritates me). ['jihan917<at>yahoo<dot>com'.replace('<at>', '@').replace('<dot>', '.')]</p>
</div>2010-11-14T09:51:35ZFrom 64.101.44.136 on /blog/python/AvoidingGlobalstag:CSpace:blog/python/AvoidingGlobals:f0eec3411e681686f370b15ab13bc71a4a3bab70From 64.101.44.136<div class="wikitext"><p>If the variable is only bound once and not changed later, it isn't stateful, and so doesn't have the problems associated with global variables.</p>
<p>For instance, if you bind some environment variables or command line arguments at global scope to variables, but don't allow mutation of them later, that's fine. They are effectively constants.</p>
<p>What's bad is when you have a piece of state that could be mutated by any line of code in your program. The point of most kinds of abstraction, if you boil it down, is to reduce the amount of code where a value could be mutated to an easily verifiable subset.</p>
</div>2010-09-01T18:45:35ZFrom 58.171.140.142 on /blog/python/AvoidingGlobalstag:CSpace:blog/python/AvoidingGlobals:ee536c6d35f0c29ec7ca4881a685a74bc5ac45a1From 58.171.140.142<div class="wikitext"><p>There's some discussion about this at <a href="http://journal.dedasys.com/2010/08/20/moving-data-around-your-code-different-approaches">Moving data around your code - different approaches</a>, via Planet Debian.</p>
<p>James</p>
</div>2010-08-31T16:32:12ZFrom 77.22.218.57 on /blog/python/AvoidingGlobalstag:CSpace:blog/python/AvoidingGlobals:c273dda9a3b11efe7e7d9809de7501eae4075c35From 77.22.218.57<div class="wikitext"><p>Another pythonic way for the case of expensive information-gathering which is needed by several (leaf-)functions is to put a memoization decorator around the information-gathering function, so that the results are only calculated on the first access.</p>
</div>2010-08-31T13:53:53Z