Re: Python scope

From: Jim Meier <jim_at_no.spam.please>
Date: Thu Jul 19 2007 - 13:03:50 CST

On 19-Jul-07, at 12:22 PM, Steven Kurylo wrote:

>> Perhaps a little odd, but its not a problem once you are used to it.
>
> I think its bad form to allow you to read a variable, but then do some
> silent slight of hand if you try to write to it. Its the silent part
> that bothers me.

Yup. It's a bit of a deviation from python's normal explicit >
implicit ideology.

> And its not universally applied either:
>
> list = [ 'a','b' ]
> def foo():
> print list
> list.append('c')
> print list
> foo()
> print list
>
> Gives
>
> ['a', 'b']
> ['a', 'b', 'c']
> ['a', 'b', 'c']
>
> So I can append to a list inside a function, so its not read only.
> Yes I understand why. Its still inconsistent.

No; the rule has applied here, of course: it's about bindings, not
about values. A python variable is a binding *only*; that the list it
is bound to is mutable does not mean you've modified the binding.

That's why the scope rule is usually not an issue: globals are
usually mutable values.

It's a bit rare for python code to be noodling with a lot of globals
- that sort of thing is usually wrapped up in a class.

> It also bothers me that I don't get a warning if I only use a variable
> once (that is, I made a typo in a variable name).

Yup. You might have a look at PyLint (http://www.logilab.org/857) or
PyChecker (http://pychecker.sourceforge.net/) for more of these sorts
of checks; the dynamic nature of the language means they're just
style checks though.

-J
Received on Thu Jul 19 13:04:31 2007

This archive was generated by hypermail 2.1.8 : Thu Jul 19 2007 - 13:04:33 CST