My idea is to support the style in which each variable comes into existence
at a single well-defined point, whatever path is taken through the code,
and so in the case of that example, it would be encouraging the use of
conditional expressions.
A worse case of what can be done with the scope rules for variables
declared as they are now is that some code paths define the variable and
some don't, so later on in the program it might or might not be defined (I
have seen this done, and find it takes extra mental work to work with).
I was thinking of all blocks defining scopes, like they do in C and many
other languages.  It would be possible to add another statement type just
for this, I suppose, and have "let x = y in:" and a suite under it.

On Mon, Jan 17, 2022 at 5:24 PM Christopher Barker <python...@gmail.com>
wrote:

> On Mon, Jan 17, 2022 at 7:36 AM John Sturdy <jcg.stu...@gmail.com> wrote:
>
>> For example, it bugs me that you can write:
>>
>> if a:
>>     b = f(x)
>> else:
>>     b = g(y)
>>
>> and get the same variable 'b' from it in the lines of code following
>> that, regardless of which path was taken.
>>
>
> Really? But that is a VERY common pattern. In fact, I would find it very
> hard to write an enormous amount of code without it. and what if you
> refactored that to be:
>
> b = f(x) if a else g(x)
>
> Do you really want that to mean something different ?!?!
>
> Or are what you are getting at that if you want those two forms to be the
> same, then you'd need to do something like:
>
> b = None
> if a:
>     b = f(x)
> else:
>     b = g(y)
>
> Which means essentially that you'd like to have to  declare names in order
> to ues them outside the very narrowest of scopes.
>
> But this is not how Python works, and it never has, it would be a really
> massive change, and I for one, would not like it :-(
>
> Also -- which blocks would create a new scope? All of them:
>
> for (else)
> while (else)
> with
> if (elif, else)
> try (except, else)
>
> I guess what I'm getting at is that I'm pretty sure I would find this
> useful rarely, and an annoyance often.
>
> - CHB
>
>
>
> --
> Christopher Barker, PhD (Chris)
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/URWFCXR3OOER3BVTIOVTAQFME6LSDQBF/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to