Re: Use of a variable in parent loop
Stephane Tougard writes: > ... > It's normal, he was an ass. When I manage a team, I don't enforce tools > or language, I ask them to work the best way they can to get the things > done. If they want to write C in Perl (as I often do), I'm happy. If > they prefer Ruby (that I never learnt), or Lisp ... as long as it works > and they are able to maintain it, I'm happy as well. > > Nothing is more enjoyable than a platform running a variety of languages > and technologies, working all together. > > And FYI, I've done this way since 25 years and I count a few good > success in my career. Practically, my way of doing things works is at least > as well as any. Maybe you seems like lisp or emacs lisp just i think, not serious... Sincerely, Byung-Hee from South Korea -- ^고맙습니다 _白衣從軍_ 감사합니다_^))// -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On Sun, Sep 27, 2020 at 03:18:44PM +0800, Stephane Tougard via Python-list wrote: > On 2020-09-27, 2qdxy4rzwzuui...@potatochowder.com > <2qdxy4rzwzuui...@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 "", line 1, in > 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. No it's not... those two words mean different things, and the difference actually matters. If you use strict in perl you need BOTH (at least, if you want your variable to actually have a value): my $foo; $foo = 1; The declaration and definition serve different purposes. You can combine them into one statement for syntactic convenience, much as you can in C/C++, but nevertheless both functions still exist and are explicitly expressed. In Python, you almost always only need the definition. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Stephane Tougard via Python-list wrote: > 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. I don't understand what you expect emacs 'indent-region' to do. With token-delimited languages, it will re-do the indentation of a block with respect to the first selected line. That's possible because for C et al. indentation can be deduced from keywords and block delimiters. That's simply not possible with Python because indentation _is_ the delimiters. Expecting Python to know how your code is supposed to be indented would be like entering a block of C code with no curly braces and then expecting emacs to know where to insert them so that the code does what you want. You can increase/decrease indentation by selecting a block and hitting C-c < or C-c >. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Terry Reedy wrote: > On 9/26/2020 3:36 PM, Stephane Tougard via Python-list wrote: >> On 2020-09-26, Terry Reedy 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. Huh? What is emacs doing to "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. It works for me. > emacs with python-mode has been and likely still is used by some > experienced python programmers. I've been usnig emacs in python-mode for almost 20 years. I've never had any problems with. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Joe Pfeiffer 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
On 2020-09-27, MRAB wrote: >> 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. I would do it in Perl, LOL. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Manfred Lotz 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
On 2020-09-27, Grant Edwards wrote: > Maybe you need to choose different editors and tools. In my world, humans don't adapt to tools but human adapt tools to their needs. > 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... It's normal, he was an ass. When I manage a team, I don't enforce tools or language, I ask them to work the best way they can to get the things done. If they want to write C in Perl (as I often do), I'm happy. If they prefer Ruby (that I never learnt), or Lisp ... as long as it works and they are able to maintain it, I'm happy as well. Nothing is more enjoyable than a platform running a variety of languages and technologies, working all together. And FYI, I've done this way since 25 years and I count a few good success in my career. Practically, my way of doing things works is at least as well as any. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On Mon, 28 Sep 2020 13:39:08 +0800 Stephane Tougard wrote: > On 2020-09-28, Manfred Lotz wrote: > > On Mon, 28 Sep 2020 05:20:20 +0800 > > Stephane Tougard wrote: > > > >> On 2020-09-27, Manfred Lotz 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. > > > > Don't understand this sentence. > > The URL you gave is pointing to your localhost, your own computer if > you prefer. It is amazing that I just didn't see it. I am a bad at spotting things. Sigh. Anyway: https://docs.python.org/3/tutorial/controlflow.html#pass-statements -- Manfred -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
Am 28.09.20 um 07:38 schrieb Stephane Tougard: On 2020-09-28, MRAB wrote: It's used where the language requires a statement. In, say, C, you would use empty braces: while (process_next_item()) { /* Do nothing. */ } If I want to express nothing in C, I put nothing and it works fine. #include int main(int argc, char * argv[]) { if(1 == 1) ; No. You put ";", that's not nothing. Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-28, Mike Dewhirst wrote: > [1] If you live with Perl non-stop I agree Perl code can be read in > future. But it requires allocation of serious brain-space for me at > least to come back to it. Let's be franc, I can read my own Perl code (very C-ish) without any issue. I live with it since 20 years. But I've seen some Perl code that is total gebbiresh to me, still doing the job correctly. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-28, Cameron Simpson wrote: > That said, Stephane: I don't believe in "best practice" as _the_ best > practice, but I certainly believe there's "bad practice". I kind of disagree with that, what I mean that there is no bad practice to get the work done. There may be bad practice to write a code that you intend to share or as part of a bigger project. Still, that depends how you deliver your code. For example, in Perl I sometime make extensive usage of goto(), through this is usually considered bad practice I've read several documents who stated otherwise. Using goto() may make the code much simpler to understand when you encounter a lot of error cases in a code. The try: except: of Python, as I understand it, is never more than a goto() in disguise (at least, that's the way I use goto() in Perl). -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-28, Manfred Lotz wrote: > On Mon, 28 Sep 2020 05:20:20 +0800 > Stephane Tougard wrote: > >> On 2020-09-27, Manfred Lotz 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. > > Don't understand this sentence. The URL you gave is pointing to your localhost, your own computer if you prefer. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-28, MRAB wrote: > It's used where the language requires a statement. > > In, say, C, you would use empty braces: > > while (process_next_item()) { > /* Do nothing. */ > } If I want to express nothing in C, I put nothing and it works fine. #include int main(int argc, char * argv[]) { if(1 == 1) ; printf("Hello\n"); return 0; } > while process_next_item(): > # Do nothing. > pass while p(): # do nothing continue -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 28/09/2020 12:56 pm, Stephane Tougard via Python-list wrote: > On 2020-09-27, Cameron Simpson 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 > Totally agree, it's not an acceptable behavior. > > I would not do some Perl without 'use strict' I came to Python from Pascal, Perl and PHP. For a while my code was full of constructs which came from those languages. As time passed and I lurked on mailing lists I began to see the power of Python. It is incredibly flexible. You can write code the way you used to or you can gradually try to write more "Pythonically". The real beauty of Python in my opinion is genuine OO. And the community. Perl can be extremely terse, much more so than Python but that is where the "write-only" criticism comes from. Perl's terseness can only work for throwaway code.[1] Python is - or can be - quite terse but if written Pythonically is almost self-documenting. You, or anyone else can read it in coming years. Cheers Mike [1] If you live with Perl non-stop I agree Perl code can be read in future. But it requires allocation of serious brain-space for me at least to come back to it. -- Signed email is an absolute defence against phishing. This email has been signed with my private key. If you import my public key you can automatically decrypt my signature and be sure it came from me. Just ask and I'll send it to you. Your email software can handle signing. signature.asc Description: OpenPGP digital signature -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 28Sep2020 13:43, Chris Angelico wrote: >On Mon, Sep 28, 2020 at 12:31 PM Stephane Tougard via Python-list > wrote: >> To be frank, I don't really care the rules and supposed best practices, >> I use a language the way it fits me. So I'll pass on the advices like >> "we never use this like this" without more reason that "we just don't do >> it" or "it's not expected to be done this way". > >If you're going to ignore advice on how to use Python well and just >use it your own way, that's fine, but don't expect python-list to help >you do things your own way. :) Also, please don't try to collaborate >with anyone unless you're prepared to follow best prac. Chris: that's not what Stephane said. Stephane passes on advice unsupported by reasons. That said, Stephane: I don't believe in "best practice" as _the_ best practice, but I certainly believe there's "bad practice". Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On Mon, Sep 28, 2020 at 12:31 PM Stephane Tougard via Python-list wrote: > To be frank, I don't really care the rules and supposed best practices, > I use a language the way it fits me. So I'll pass on the advices like > "we never use this like this" without more reason that "we just don't do > it" or "it's not expected to be done this way". If you're going to ignore advice on how to use Python well and just use it your own way, that's fine, but don't expect python-list to help you do things your own way. :) Also, please don't try to collaborate with anyone unless you're prepared to follow best prac. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-28 03:53, Stephane Tougard via Python-list wrote: On 2020-09-27, Chris Angelico wrote: Or maybe Emacs *isn't* breaking it, and it's just an autoindentation thing. I don't know. From the discussion I read about this feature, it considers that 'pass' is use to write an empty def() def(); pass So it's logic for it to indent one level up after a 'pass' because the people who made it did not see any other usage to 'pass' than that. if True: pass print("It's true") The 'pass' is totally useless by itself, it can be replaced by a comment. It's used where the language requires a statement. In, say, C, you would use empty braces: while (process_next_item()) { /* Do nothing. */ } In Python that would be: while process_next_item(): # Do nothing. pass -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On Mon, 28 Sep 2020 05:20:20 +0800 Stephane Tougard wrote: > On 2020-09-27, Manfred Lotz 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. Don't understand this sentence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Chris Angelico wrote: > Or maybe Emacs *isn't* breaking it, and it's just an autoindentation > thing. I don't know. >From the discussion I read about this feature, it considers that 'pass' is use to write an empty def() def(); pass So it's logic for it to indent one level up after a 'pass' because the people who made it did not see any other usage to 'pass' than that. if True: pass print("It's true") The 'pass' is totally useless by itself, it can be replaced by a comment. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Cameron Simpson 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 Totally agree, it's not an acceptable behavior. I would not do some Perl without 'use strict' -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Terry Reedy wrote: > emacs with python-mode has been and likely still is used by some > experienced python programmers. I have never seen anyone but a rank Yes, since I discovered that an empty has almost the same effect than a pass to end a block. > The 'pass' line does not mark the end of the if block. Yes, I know, I discovered that yesterday. Reading the documentation, it should not anyway. The way Emacs understands it is misleading. > Python only has a temporary overlap mechnism: That's the only answer I was expecting instead of all this arguing as if I said any blasphem because I asked how can we change the behavior of the language. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 2020-09-27, Avi Gross wrote: > But when someone insists Python needs to > change to meet their preconception, I get less sympathetic. To clarify my question, I never asked that Python changes for me, I asked if there was any way to change Python's behavior by using a module or a configuration for example. As "use strict;" in Perl or JS who changes the behavior of the language. That's kind of difference between Perl and Python (or C and GO) is that each Mongiste has its own style and uses Perl the way it fits for his needs, while Pythonist has to write Python as it has been defined (this rule is much stronger with GO). To be frank, I don't really care the rules and supposed best practices, I use a language the way it fits me. So I'll pass on the advices like "we never use this like this" without more reason that "we just don't do it" or "it's not expected to be done this way". Anyway, thanks for your help. -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On 27/09/20 10:27 pm, Stephane Tougard wrote: 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. I doubt whether the authors of the Emacs python-mode had this way of using "pass" in mind. More likely they're just trying to be helpful and save you from having to manually unindent in a few rare situations. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On Sun, Sep 27, 2020 at 9:41 PM Manfred Lotz wrote: > > On Sun, 27 Sep 2020 15:18:44 +0800 > Stephane Tougard wrote: > > > On 2020-09-27, 2qdxy4rzwzuui...@potatochowder.com > > <2qdxy4rzwzuui...@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 "", line 1, in > > 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 > Looks like you're linking to a local copy of the documentation. Here's the same page from the official docs: https://docs.python.org/3/tutorial/controlflow.html#pass-statements (It'll be the same information, modulo version differences, but this one should be available everywhere.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
On Mon, Sep 28, 2020 at 7:50 AM Stephane Tougard via Python-list wrote: > > On 2020-09-27, Stefan Ram 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
Re: Use of a variable in parent loop
On 2020-09-27 10:27, Stephane Tougard via Python-list wrote: On 2020-09-27, Stefan Ram 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
On 2020-09-27, Stefan Ram 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
On 9/26/2020 3:36 PM, Stephane Tougard via Python-list wrote: On 2020-09-26, Terry Reedy 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
On 2020-09-27, Chris Angelico 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
On 2020-09-27, Stephane Tougard via Python-list 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
On 27Sep2020 15:18, Stephane Tougard 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Use of a variable in parent loop
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) > in () > > 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
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) in () > 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
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 On Behalf Of 2qdxy4rzwzuui...@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 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 lea
Re: Use of a variable in parent loop
Stephane Tougard writes: > On 2020-09-27, Stefan Ram 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
On 2020-09-27 at 15:18:44 +0800, Stephane Tougard via Python-list 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
On Sun, 27 Sep 2020 15:18:44 +0800 Stephane Tougard wrote: > On 2020-09-27, 2qdxy4rzwzuui...@potatochowder.com > <2qdxy4rzwzuui...@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 "", line 1, in > 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
On Sun, Sep 27, 2020 at 9:01 PM Stephane Tougard via Python-list wrote: > > On 2020-09-27, 2qdxy4rzwzuui...@potatochowder.com > <2qdxy4rzwzuui...@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 "", line 1, in > 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
On Sun, Sep 27, 2020 at 9:01 PM Stephane Tougard via Python-list wrote: > > On 2020-09-27, Stefan Ram 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
On 2020-09-27, Stefan Ram 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
On 2020-09-27, 2qdxy4rzwzuui...@potatochowder.com <2qdxy4rzwzuui...@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 "", line 1, in 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
On 2020-09-27 at 03:36:48 +0800, Stephane Tougard via Python-list 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
On 2020-09-26, Terry Reedy 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
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
On Sat, Sep 26, 2020 at 4:01 PM Stephane Tougard via Python-list 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
On Sat, Sep 26, 2020 at 2:01 AM Stephane Tougard via Python-list 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
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