Keen eyes

Chris Angelico rosuav at gmail.com
Sun Jan 17 06:32:49 EST 2016


On Sun, Jan 17, 2016 at 10:17 PM, BartC <bc at freeuk.com> wrote:
> On 17/01/2016 08:25, Chris Angelico wrote:
>>
>> On Sun, Jan 17, 2016 at 6:37 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>>
>>> Technically it defaults to non local. The var statement allocates a
>>> variable within the current scope. Otherwise it searches up the chain of
>>> parent scopes for a matching variable...
>>
>>
>> This far, it's exactly the same as C.
>>
>>> ... terminating at the global scope.
>>
>>
>> This is the bit that gets ridiculous. Undeclared variables in C are
>> compile-time errors. Undeclared variables in JS are implicit globals.
>> This is stupid.
>
>
> My own language is something in-between. If a name is not declared locally,
> it will look at more global scopes. If nothing is found, it will
> auto-declare a local.
>
> So the JS bug wouldn't occur. However, there is the problem that, given a
> perfectly working function with implicitly locals, at some point in the
> future someone could introduce a global that will clash with the name of a
> local, and screw things up.
>
> Because of that, the Python scheme is better on the whole. The only issue is
> that sometimes you think you're assigning to a global, but it's really a
> local if you forget the 'global' declaration within the function.

The Python scheme makes a lot of logical sense. The C scheme (declare
everything, otherwise it's an error) makes a lot of logical sense.
I've never seen a hybrid scheme that makes as much sense as either of
the above. In both Python's and C's ways of doing things, you can look
at a single function and know which names are local and which are
external. Have fun trying to share code snippets without knowing full
context...

ChrisA



More information about the Python-list mailing list