<br><div class="gmail_quote">On Mon, May 30, 2011 at 5:28 PM, Henry Olders <span dir="ltr"><<a href="mailto:henry.olders@mcgill.ca">henry.olders@mcgill.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
What I would like is that the variables which are included in the function definition's parameter list, would be always treated as local to that function (and of course, accessible to nested functions) but NOT global unless explicitly defined as global.<br>
</blockquote><div><br>If you don't want any globals other than callables and modules and builtins, then don't define any. I normally code that way, other than an infrequent (EG) HAVE_CSTRINGIO global or similar (which is module-global, not program-global) - that is, for variables describing what sorts of optional modules are available.<br>
<br>Be careful not to conflate global scoping or global lifetime, with mutability or pure, side-effect-free functions (callables). It sounds like what you want is immutability and/or freedom from side effects, which is found most often in (pure) functional languages - which is not what Python is, nor does it attempt to be so.<br>
<br>In Python, and in many other languages, if you pass a scalar (or more generally, an object of an immutable type) to a function, and then change the scalar in that function, you only change the scalar within that function, not within the caller.<br>
<br>However, if you pass an aggregate type like a list (array) or dictionary (hash table), then the formal argument itself that you've passed is still only changeable within that function, however what it points off at _is_ changeable via that formal argument. This is of course because otherwise passing a 1 gigabyte dictionary to a function would either have to copy the whole dictionary, or implement some sort of Copy-on-Write semantics, or make it somehow readonly.<br>
<br>If you need side effect-free functions in Python, you'd probably best copy your aggregates, EG:<br><br>import copy<br><br>def main():<br> a = ['a list','with','three elements']<br> print a<br>
print fnc1(a)<br> print a<br><br>def fnc1(b):<br> b = copy.deepcopy(b)<br> return fnc2(b)<br><br>def fnc2(c):<br> c = copy.deepcopy(c)<br> c[1] = 'having'<br> return c<br><br>main()<br>
<br>Or use Haskell. Please don't try to turn Python into Haskell. Each has its own interesting tradeoffs, and we don't really need two Pythons, and we don't really need two Haskells.<br><br>HTH.<br><br></div>
</div>