Mailing List Archive

Use of a variable in parent loop
Hello All,

I've been working with Perl a long time and recently started to use
Python. I've been surprised by one behavior of Python.

In Perl:

===PERL===
#!/usr/pkg/bin/perl

use strict;

if(4 == 4)
{
my $name = "Stephane";
print("$name\n"
}
print("Out $name\n");
=========

This code will trigger an error because $name is declared inside the if
and is not usable outside of the block code. That looks logic to me.

===PYTHON===
#!/usr/local/bin/python
if 4 == 4:
name = "Stephane"
print(name)
pass

print("Out {}".format(name))
============

The exact same code in Python works fine, the variable name is used
outside of the if block even it has been declared inside.

This does not look right to me. Can we change this behavior or is there
any point to keep it this way ?
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 9/26/2020 12:43 AM, Stephane Tougard via Python-list wrote:

[Example of Perl block scoping.]

> ===PYTHON===
> #!/usr/local/bin/python
> if 4 == 4:

if True: # Only usefel in Python if you might might to switch to False.

> name = "Stephane"
> print(name)
> pass

Noise. Only 'pass' when there is no other code.

> print("Out {}".format(name))
> ============
>
> The exact same code in Python works fine, the variable name is used
> outside of the if block even it has been declared inside.

'name' is bound to a object, not declared
The only declarations are 'global' and 'nonlocal'.

Python only creates new scopes for class and function definitions. At
least for CPython, comprehensions are implicit function definitions.

> This does not look right to me.

Because you are accustomed to block scoping.

Can we change this behavior

No.

> or is there any point to keep it this way ?

Aside from not breaking most every existing Python program? If block
scoped, one would have to add an otherwise useless fake declaration
before the block to use the name outside the block. Python tries to
avoid boilerplate code.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On Sat, Sep 26, 2020 at 2:01 AM Stephane Tougard via Python-list
<python-list@python.org> wrote:
>
>
> Hello All,
>
> I've been working with Perl a long time and recently started to use
> Python. I've been surprised by one behavior of Python.
>
> In Perl:
>
> ===PERL===
> #!/usr/pkg/bin/perl
>
> use strict;
>
> if(4 == 4)
> {
> my $name = "Stephane";
> print("$name\n"
> }
> print("Out $name\n");
> =========
>
> This code will trigger an error because $name is declared inside the if
> and is not usable outside of the block code. That looks logic to me.
>
> ===PYTHON===
> #!/usr/local/bin/python
> if 4 == 4:
> name = "Stephane"
> print(name)
> pass
>
> print("Out {}".format(name))
> ============
>
> The exact same code in Python works fine, the variable name is used
> outside of the if block even it has been declared inside.
>
> This does not look right to me. Can we change this behavior or is there
> any point to keep it this way ?
> --
> https://mail.python.org/mailman/listinfo/python-list


It looks like perl creates a local namespace inside the code suite of
the if statement. Python namespace is function based.

--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On Sat, Sep 26, 2020 at 4:01 PM Stephane Tougard via Python-list
<python-list@python.org> wrote:
>
>
> Hello All,
>
> I've been working with Perl a long time and recently started to use
> Python. I've been surprised by one behavior of Python.
>
> In Perl:
>
> ===PERL===
> #!/usr/pkg/bin/perl
>
> use strict;
>
> if(4 == 4)
> {
> my $name = "Stephane";
> print("$name\n"
> }
> print("Out $name\n");
> =========
>
> This code will trigger an error because $name is declared inside the if
> and is not usable outside of the block code. That looks logic to me.
>
> ===PYTHON===
> #!/usr/local/bin/python
> if 4 == 4:
> name = "Stephane"
> print(name)
> pass
>
> print("Out {}".format(name))
> ============
>
> The exact same code in Python works fine, the variable name is used
> outside of the if block even it has been declared inside.
>
> This does not look right to me. Can we change this behavior or is there
> any point to keep it this way ?

Just to get this part out of the way: No, that behaviour won't change. :)

The distinction is that, in Python, you didn't declare the variable at
all. It's not like C where you have to declare everything, and the
place it's declared defines the scope; in Python, variables generally
*aren't* declared, and there are a small number of cases where you
declare that something's a global when it would otherwise be local.
But for the most part, Python's default rules handle the situations
admirably.

Most variables are either global (if you assign something outside of
any function) or local (inside a function). There's no concept of
scoping a variable to just a single 'if' statement, so you don't have
to worry about it disappearing prematurely.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-26, Terry Reedy <tjreedy@udel.edu> wrote:
> Noise. Only 'pass' when there is no other code.

Why ?

I use pass and continue each time to break a if or a for because emacs
understands it and do not break the indentation.

Is there any other instruction to end a if than pass and ensure Emacs
does not break the indentation during a copy paste or an indent-region ?

> Aside from not breaking most every existing Python program? If block
> scoped, one would have to add an otherwise useless fake declaration
> before the block to use the name outside the block. Python tries to
> avoid boilerplate code.

I'm not talking about a general change in Python as a language, I'm
talking about a module who would enforce a block namespace as it works with
C or Perl (and many).

In Perl, use strict will break any non-strict code. So each is free to
chose between strict and not strict.

Anyway, there's no perfect language, the point is to know it. It's just
confusing I still have to declare or not declare an object depending on
the action I have with it.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27 at 03:36:48 +0800,
Stephane Tougard via Python-list <python-list@python.org> wrote:

> Anyway, there's no perfect language, the point is to know it ...

Yes.

> ... It's just confusing I still have to declare or not declare an
> object depending on the action I have with it.

As ChrisA noted, Python almost always Just Works without declarations.
If you find yourself with a lot of global and/or nonlocal statements,
perhaps you're [still] thinking in another language.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>>Is there any other instruction to end a if than pass and ensure Emacs
>>does not break the indentation during a copy paste or an indent-region ?
>
> We usually do not wish to tie our code to a defective editor.
> I use vi, and can assure you that there is no such restriction
> in a real editor.

You do not answer the question. I consider that indentation alone is not
enough to make the end of a block. It's not a question of editor and I
had some issues with vim as well because of that.

pass looks good to me to end a if block, continue is good to end a for
block and return is good to end a def block. If that's NOT good, just
tell me why and give me another solution to end a block who is not the
indentation because an indentation is not good enough for me to end a
block and it may trigger some problem when using different editors or
tools on the code.

> Objects never are declared, at most names are declared.
> /First/ learn a language, /then/ suggest changes.

Are all pythonist such pain in the ass or are you an exception ?

I've never suggesting to change the language, I'm asking if there is way
to do things.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27, 2QdxY4RzWzUUiLuE@potatochowder.com <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
> As ChrisA noted, Python almost always Just Works without declarations.
> If you find yourself with a lot of global and/or nonlocal statements,
> perhaps you're [still] thinking in another language.


I don't really agree with that, trying to use an undeclared
object/variable/whatever :

Python 3.7.7 (default, Aug 22 2020, 17:07:43)
[GCC 7.4.0] on netbsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> print(name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>>

You can say it's not the "declaration" the issue, it's the "definition",
that's just a matter of vocabulary and it does not answer the question.

In many non declarative language, if I do print($var), it just prints
and undefined value with returning an error.


--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>>Is there any other instruction to end a if than pass and ensure Emacs
>>does not break the indentation during a copy paste or an indent-region ?
>
> We usually do not wish to tie our code to a defective editor.
> I use vi, and can assure you that there is no such restriction
> in a real editor.

It's funny, I've made a few tests and I see that pass has no impact on
if block in fact, I can put it anywhere and add code behind, it works
fine. I was sure that pass was breaking the if block, it does not.

That's however still the way Emacs seems to see it (I can not add
any code in a if block after a pass, pass acts as if it breaks the if
block).

As pass does nothing anyway, that looks like a good way to mark the end
of a block and I did not find any valid reason to not use it this way.
That's not a question of editor, that's a question of having a clear way
to mark the end of a block, I guess the Emacs maintener found this way
and I think it's a great idea.

If a extremist Pythonist takes over my code some day, he'll have to
search and delete hundreds of useless pass. I laugh already thinking
about it.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On Sun, Sep 27, 2020 at 9:01 PM Stephane Tougard via Python-list
<python-list@python.org> wrote:
>
> On 2020-09-27, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> >>Is there any other instruction to end a if than pass and ensure Emacs
> >>does not break the indentation during a copy paste or an indent-region ?
> >
> > We usually do not wish to tie our code to a defective editor.
> > I use vi, and can assure you that there is no such restriction
> > in a real editor.
>
> You do not answer the question. I consider that indentation alone is not
> enough to make the end of a block. It's not a question of editor and I
> had some issues with vim as well because of that.

Why? What benefit do you gain by having a keyword like that?
Indentation DOES end a block, and adding a noise word just before you
unindent just leaves the possibility for it to be incorrect and thus
misleading - not to mention that it's fundamentally misleading to
anyone who is accustomed to Python syntax and the normal use of those
keywords.

If you MUST use a block-end marker, try "# end" instead - at least
then everyone *knows* it's nothing more than a comment.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On Sun, Sep 27, 2020 at 9:01 PM Stephane Tougard via Python-list
<python-list@python.org> wrote:
>
> On 2020-09-27, 2QdxY4RzWzUUiLuE@potatochowder.com <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
> > As ChrisA noted, Python almost always Just Works without declarations.
> > If you find yourself with a lot of global and/or nonlocal statements,
> > perhaps you're [still] thinking in another language.
>
>
> I don't really agree with that, trying to use an undeclared
> object/variable/whatever :
>
> Python 3.7.7 (default, Aug 22 2020, 17:07:43)
> [GCC 7.4.0] on netbsd9
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print(name)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'name' is not defined
> >>>
>
> You can say it's not the "declaration" the issue, it's the "definition",
> that's just a matter of vocabulary and it does not answer the question.
>
> In many non declarative language, if I do print($var), it just prints
> and undefined value with returning an error.
>

>>> name = "Fred"
>>> print(name)
Fred

Lack of declaration doesn't imply that every variable is initialized
to some null value.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On Sun, 27 Sep 2020 15:18:44 +0800
Stephane Tougard <stephane@sdf.org> wrote:

> On 2020-09-27, 2QdxY4RzWzUUiLuE@potatochowder.com
> <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
> > As ChrisA noted, Python almost always Just Works without
> > declarations. If you find yourself with a lot of global and/or
> > nonlocal statements, perhaps you're [still] thinking in another
> > language.
>
>
> I don't really agree with that, trying to use an undeclared
> object/variable/whatever :
>
> Python 3.7.7 (default, Aug 22 2020, 17:07:43)
> [GCC 7.4.0] on netbsd9
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print(name)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'name' is not defined
> >>>
>
> You can say it's not the "declaration" the issue, it's the
> "definition", that's just a matter of vocabulary and it does not
> answer the question.
>
> In many non declarative language, if I do print($var), it just prints
> and undefined value with returning an error.
>
>

It is very good that you try out things. Nevertheless, it is also good
to read. In other words the combination of reading and trying out
things is the best way to make progress.

Here some pointers

pass

- http://localhost:2015/tutorial/controlflow.html#pass-statements

about variables

- https://realpython.com/python-variables/

Regarding your question above: As long as `name` is not attached to an
object it doesn't exist and the error message above is correct.

You have to assign a value (or object as everything is an object in
Python) to `name`, Then you can use it.


(In comparison to guys like ChrisA and StefanR and others here I am also
a Python beginner)


--
Manfred

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27 at 15:18:44 +0800,
Stephane Tougard via Python-list <python-list@python.org> wrote:

> In many non declarative language, if I do print($var), it just prints
> and undefined value with returning an error.

If I want "many non declarative language[s]," I know where to find them,
and I won't expect them to honor Python's semantics. In Python, a name
doesn't have a value unless and until you bind it to one.

There's a lot more to learning a new language than the syntax.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27, Chris Angelico <rosuav@gmail.com> wrote:
> If you MUST use a block-end marker, try "# end" instead - at least
> then everyone *knows* it's nothing more than a comment.

Damn, you could not say that earlier !!!
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27, Stephane Tougard via Python-list <python-list@python.org> wrote:

> an indentation is not good enough for me to end a block

Maybe you need to choose a different language -- one that has block
delimiter keywords or tokens.

> and it may
> trigger some problem when using different editors or tools on the
> code.

Maybe you need to choose different editors and tools.

A guy I worked for many years ago used to write BASIC programs in C by
using a bizarre set of pre-processor macros. While it provided his
employees with plenty of amusement, he could never get the programs to
work right...



--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 9/26/2020 3:36 PM, Stephane Tougard via Python-list wrote:
> On 2020-09-26, Terry Reedy <tjreedy@udel.edu> wrote:
>> Noise. Only 'pass' when there is no other code.
>
> Why ?

>
> I use pass and continue each time to break a if or a for because emacs
> understands it and do not break the indentation.
>
> Is there any other instruction to end a if than pass and ensure Emacs
> does not break the indentation during a copy paste or an indent-region ?

Emacs should come with python.el or python-mode.el defining a
python-mode. Are you using it? I presume it understands python block
structure without extra passes.

emacs with python-mode has been and likely still is used by some
experienced python programmers. I have never seen anyone but a rank
beginner misunderstanding 'pass' misusing it as an end-block marker.
(Ditto for putting ';' at the end of every line.) Dedents or EOF do that.

if a:
b = 3
pass
c = 5
else:
b = 1
c = 2

The 'pass' line does not mark the end of the if block.

>> Aside from not breaking most every existing Python program? If block
>> scoped, one would have to add an otherwise useless fake declaration
>> before the block to use the name outside the block. Python tries to
>> avoid boilerplate code.
>
> I'm not talking about a general change in Python as a language, I'm
> talking about a module who would enforce a block namespace as it works with
> C or Perl (and many).

The existence of a permanent alternate syntax mechanism would be a
general change to the language.

Python only has a temporary overlap mechnism:

from __future__ import new_syntax.

This gives people usually 2 versions to adjust to a new syntax that
breaks compatibility by switching to the new syntax anytime before it
becomes mandatory.

For example, making 'with' a keyword broke any other use of the word.
So people temporarily had to add
from __future__ import with_statement
at the top of a file to use with statements in that file.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
Stephane Tougard <stephane@sdf.org> writes:

> On 2020-09-27, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>>>Is there any other instruction to end a if than pass and ensure Emacs
>>>does not break the indentation during a copy paste or an indent-region ?
>>
>> We usually do not wish to tie our code to a defective editor.
>> I use vi, and can assure you that there is no such restriction
>> in a real editor.
>
> You do not answer the question. I consider that indentation alone is not
> enough to make the end of a block. It's not a question of editor and I
> had some issues with vim as well because of that.
>
> pass looks good to me to end a if block, continue is good to end a for
> block and return is good to end a def block. If that's NOT good, just
> tell me why and give me another solution to end a block who is not the
> indentation because an indentation is not good enough for me to end a
> block and it may trigger some problem when using different editors or
> tools on the code.

The language designers appear to disagree with you.

Decades ago I learned C after having first become proficient in Pascal,
and *really* didn't like the terseness of the block beginning and ending
symbols.

So, I went through a phase in which I wrote lots of macroes like
#define begin {
#define end }

and so forth. What I discovered in fairly short order was that it made
it easier for me to read my own code, but did absolutely nothing for
either me reading other people's code, nor for them reading mine. I
eventually concluded my best move was to just suck it up and learn to
program in the language as intended.
--
https://mail.python.org/mailman/listinfo/python-list
RE: Use of a variable in parent loop [ In reply to ]
These discussions can be bit frustrating. People talk past each other.
Languages seem almost like religions, as do paradigms.

I try to stay above it and absorb each paradigm as an alternative choice
made and then try to think of what problems I can solve better with one or
another and so on.

Programming languages are often created when people have new ideas and want
to try them out, often keeping a good chunk of the language similar enough
to other languages so they seem partially familiar and then diverge.

Whether a particular text editor has a mode than honors your typing in a
language is a side issue. Whether you can use cut and paste properly is a
similar side issue. Python chose indentation so you can remove lots of curly
braces and often semi-colons that clutter other languages. There is a
trade-off. In general, you can use superfluous symbols harmlessly such as
curly braces above and below a region so that it looks like a BLOCK but that
won't make it a block for purposes that language does not consider a good
feature.

And another trade-off is that it is not really a compiled language and
variables are looked up dynamically in a varying path of namespaces and the
same name can constantly change what kinds of objects it holds. There is
lots of freedom there that I appreciate as compared to some C-inspired
languages including GO that I am learning now, just for fun. I do lots of
prototyping and that is easier to do when less constrained but a final
program may well run better in a compiled form.

But like human languages, some people may not easily be able to switch to a
new one that violates what to them seem obvious rules. I was exposed to many
in childhood so some sounds are easy for me to hear and pronounce that
others cannot deal with but I am apparently tone deaf when I try to learn a
language like Mandarin that uses some kind of "tones" and certainly am not
willing to learn an alphabet that is not sound-based and has tens of
thousands of characters. But, I seem able to handle many alphabets that exit
in European and Semitic languages even when they use the same apparent
letters in very different ways. To each their own.

So for those replying here that the person asking a question needs to drop
their preconceptions and instead learn what Python IS DOING, I fully agree
as long as it is done gently. But when someone insists Python needs to
change to meet their preconception, I get less sympathetic. A language that
has been around for decades is often hard to change without breaking things
and we know Python 2.X and earlier are not particularly compatible and what
trouble that has caused. Other languages like PERL have had such schisms and
growing pains. Sometimes a better solution is to start a new language from
scratch and just keep what you want and freely add new.

Dumb example, perhaps, is features I see in GO. Some are quite interesting
but I am not demanding they be added to Python or R or any other language I
use. Heck, some may mess up existing programs. Go has a "defer" statement
that creates a stack of function calls to be done AFTER the function
creating them returns. While that can be nifty, it is a very different
paradigm. But a different paradigm in say R allows you to specify what must
be done on exit from a function even if it exist badly. Python has variants
as well including for some protocols that close open files if used in a
"with" and so on. Lots of ideas out there but you have to use what your
language comes with to a point, or work out your own variants of objects or
add wrappers around functions and so on. Python has tons of such techniques
if you read the documentation and other literature.

And, yes, there are text editors designed better to handle writing code in
python and also external utilities you can use to reformat the code in many
cases to your needs.

I have found that in programming IDEAS, there are many forks in the road as
design decisions you make and amazingly often you can find one or more
programming environments where one or another fork has been taken. There
often is no one right answer that everyone agrees with and choices are often
made for other reasons such as how easy the creators can implement things
even if the result is harder to program some things in.

Avi

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net@python.org> On
Behalf Of 2QdxY4RzWzUUiLuE@potatochowder.com
Sent: Sunday, September 27, 2020 8:17 AM
To: python-list@python.org
Subject: Re: Use of a variable in parent loop

On 2020-09-27 at 15:18:44 +0800,
Stephane Tougard via Python-list <python-list@python.org> wrote:

> In many non declarative language, if I do print($var), it just prints
> and undefined value with returning an error.

If I want "many non declarative language[s]," I know where to find them, and
I won't expect them to honor Python's semantics. In Python, a name doesn't
have a value unless and until you bind it to one.

There's a lot more to learning a new language than the syntax.
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
Am 26.09.20 um 06:43 schrieb Stephane Tougard:
> ===PYTHON===
> #!/usr/local/bin/python
> if 4 == 4:
> name = "Stephane"
> print(name)
> pass
>
> print("Out {}".format(name))
> ============
>
> The exact same code in Python works fine, the variable name is used
> outside of the if block even it has been declared inside.
>
> This does not look right to me.

I'll try another way of explaining it. You seem to be confused that the
scope of the variable assignment[*] continues after the if. However,
look at this:

def f():
a=3

f()
a

NameError
Traceback (most recent call last)
<ipython-input-3-60b725f10c9c> in <module>()
----> 1 a

NameError: name 'a' is not defined

So the "def f()" obviously introduces local scope, but control
structures like if and while do not.

Christian


[*] In Python it's called "name binding", but it mostly works like
variable assignment
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 9/27/20 4:42 PM, Christian Gollwitzer wrote:
> Am 26.09.20 um 06:43 schrieb Stephane Tougard:
>> ===PYTHON===
>> #!/usr/local/bin/python
>> if 4 == 4:
>>      name = "Stephane"
>>      print(name)
>>      pass
>>
>> print("Out {}".format(name))
>> ============
>>
>> The exact same code in Python works fine, the variable name is used
>> outside of the if block even it has been declared inside.
>>
>> This does not look right to me.
>
> I'll try another way of explaining it. You seem to be confused that
> the scope of the variable assignment[*] continues after the if.
> However, look at this:
>
> def f():
>     a=3
>
> f()
> a
>
> NameError
> Traceback (most recent call last)
> <ipython-input-3-60b725f10c9c> in <module>()
> ----> 1 a
>
> NameError: name 'a' is not defined
>
> So the "def f()" obviously introduces local scope, but control
> structures like if and while do not.
>
>     Christian
>
>
> [*] In Python it's called "name binding", but it mostly works like
> variable assignment

Yes, functions and classes have a scope, control structures do not. If
control structures created a scope it would be ugly.

You do need to watch out about the difference between classical
'variables' and pythons name binding when you deal with mutable objects;

For example:

a = []

b = a

a.append(1)

print(b)

give [1] as a and b are bound to the same object, even though you want
to think of them as different variables.

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27, Manfred Lotz <ml_news@posteo.de> wrote:
> - http://localhost:2015/tutorial/controlflow.html#pass-statements
...
> (In comparison to guys like ChrisA and StefanR and others here I am also
> a Python beginner)

To give me a pointer on your localhost, I could guess.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 27Sep2020 15:18, Stephane Tougard <stephane@sdf.org> wrote:
>In many non declarative language, if I do print($var), it just prints
>and undefined value with returning an error.

And that way lie MANY MANY bugs not detected until an undefined value
actually causes an issue, if that ever happens. In some languages
undefined values are quietly promotes to eg 0 in a numeric context. No
errors, no exceptions, just silent incorrect results.

In Python the choice was made to raise an error for use of a variable
not previously bound.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
> and so forth. What I discovered in fairly short order was that it made
> it easier for me to read my own code, but did absolutely nothing for
> either me reading other people's code, nor for them reading mine. I
> eventually concluded my best move was to just suck it up and learn to
> program in the language as intended.

Not that I disagree, but coming from twenty years of Perl, it means
where nobody really understands the code of anybody else, that never has
really been a concern to me.

However, I discovered that Emacs interprets as well an empty line or a
comment as a breaking point of a block, it's not as good as the use of
pass because I still have to indent up manually, but at least the
indent-region does not break it.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On 2020-09-27 10:27, Stephane Tougard via Python-list wrote:
> On 2020-09-27, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>>>Is there any other instruction to end a if than pass and ensure Emacs
>>>does not break the indentation during a copy paste or an indent-region ?
>>
>> We usually do not wish to tie our code to a defective editor.
>> I use vi, and can assure you that there is no such restriction
>> in a real editor.
>
> It's funny, I've made a few tests and I see that pass has no impact on
> if block in fact, I can put it anywhere and add code behind, it works
> fine. I was sure that pass was breaking the if block, it does not.
>
> That's however still the way Emacs seems to see it (I can not add
> any code in a if block after a pass, pass acts as if it breaks the if
> block).
>
> As pass does nothing anyway, that looks like a good way to mark the end
> of a block and I did not find any valid reason to not use it this way.
> That's not a question of editor, that's a question of having a clear way
> to mark the end of a block, I guess the Emacs maintener found this way
> and I think it's a great idea.
>
> If a extremist Pythonist takes over my code some day, he'll have to
> search and delete hundreds of useless pass. I laugh already thinking
> about it.
>
He could write some code to do it.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop [ In reply to ]
On Mon, Sep 28, 2020 at 7:50 AM Stephane Tougard via Python-list
<python-list@python.org> wrote:
>
> On 2020-09-27, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> >>Is there any other instruction to end a if than pass and ensure Emacs
> >>does not break the indentation during a copy paste or an indent-region ?
> >
> > We usually do not wish to tie our code to a defective editor.
> > I use vi, and can assure you that there is no such restriction
> > in a real editor.
>
> It's funny, I've made a few tests and I see that pass has no impact on
> if block in fact, I can put it anywhere and add code behind, it works
> fine. I was sure that pass was breaking the if block, it does not.
>
> That's however still the way Emacs seems to see it (I can not add
> any code in a if block after a pass, pass acts as if it breaks the if
> block).

Well, then Emacs is the thing breaking it, because the 'pass'
statement has absolutely no code associated with it. It's equivalent
to an empty pair of braces in C.

Or maybe Emacs *isn't* breaking it, and it's just an autoindentation
thing. I don't know.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list

1 2  View All