Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-08 Thread Chris Angelico via Python-list
On Sat, 9 Mar 2024 at 03:42, Grant Edwards via Python-list wrote: > > On 2024-03-08, Chris Angelico via Python-list wrote: > > On Sat, 9 Mar 2024 at 00:51, Grant Edwards via Python-list > > wrote: > > > >> One might argue that "global" isn't a good choice for what to call the > >> scope in questi

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-08 Thread Grant Edwards via Python-list
On 2024-03-08, Chris Angelico via Python-list wrote: > On Sat, 9 Mar 2024 at 00:51, Grant Edwards via Python-list > wrote: > >> One might argue that "global" isn't a good choice for what to call the >> scope in question, since it's not global. It's limited to that source >> file. It doesn't make s

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-08 Thread Chris Angelico via Python-list
On Sat, 9 Mar 2024 at 00:51, Grant Edwards via Python-list wrote: > One might argue that "global" isn't a good choice for what to call the > scope in question, since it's not global. It's limited to that source > file. It doesn't make sense to me to call a binding "global", when > there can be mul

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-08 Thread Grant Edwards via Python-list
On 2024-03-07, Cameron Simpson via Python-list wrote: > Yes. Note that the "global" namespace is the module in which the > function is defined. One might argue that "global" isn't a good choice for what to call the scope in question, since it's not global. It's limited to that source file. It d

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-07 Thread Cameron Simpson via Python-list
On 06Mar2024 15:12, Jacob Kruger wrote: So, this does not make sense to me in terms of the following snippet from the official python docs page: https://docs.python.org/3/faq/programming.html "In Python, variables that are only referenced inside a function are implicitly global. If a variable

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-07 Thread Jacob Kruger via Python-list
Thanks again, all. I think the python -i scoping2.py would have given me a good beginning as well - will archive that one for use. And, to maybe explain how I work - not an excuse at all - but, I am actually 100% blind, so a lot of the IDE's, or their common means/methods of interaction do

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Grant Edwards via Python-list
On 2024-03-07, dn via Python-list wrote: > The idea of importing a module into the REPL and then (repeatedly) > manually entering the code to set-up and execute is unusual (surely type > such into a script (once), and run that (repeatedly). As you say, most > of us would be working from an IDE

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread dn via Python-list
On 7/03/24 05:28, Jacob Kruger via Python-list wrote: ... So, yes, know this comes across like some form of a scam/joke, or list-garbage, since it doesn't make any sense to me at all, but still just wondering if missing something, or should I shift over to 3.12 to see if if works differently, o

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Roel Schroeven via Python-list
Grant Edwards via Python-list schreef op 6/03/2024 om 18:59: On 2024-03-06, Roel Schroeven via Python-list wrote: > Op 6/03/2024 om 17:40 schreef Jacob Kruger via Python-list: >> >>> from scoping2 import * > > [...] > > I would advice not to use 'import *', if at all possible, for multiple

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Grant Edwards via Python-list
On 2024-03-06, Roel Schroeven via Python-list wrote: > Op 6/03/2024 om 17:40 schreef Jacob Kruger via Python-list: >> >>> from scoping2 import * > > [...] > > I would advice not to use 'import *', if at all possible, for multiple > reasons, one of which is to prevent problems like this. Unfortun

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Roel Schroeven via Python-list
Op 6/03/2024 om 17:40 schreef Jacob Kruger via Python-list: >>> from scoping2 import * Ah yes, that explains what's happening. After that statement, the name dt_expiry in the current namespace is bound to the same object that the name dt_expiry in the namespace of module scoping2 is bound to. F

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Jacob Kruger via Python-list
Ok, Ethan, that makes sense - I generally work with modules in folders, etc., but, this was just test code, but, 'see' if I instead import scoping2 as sc2, and then refer to sc2.dt_expiry and sc2.do_it, then it does operate as it should - thanks, again. Jacob Kruger +2782 413 4791 "Resistance

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Ethan Furman via Python-list
On 3/6/24 08:28, Jacob Kruger via Python-list wrote: > C:\temp\py_try>python > Python 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> from scoping2 import * And it becomes c

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Jacob Kruger via Python-list
You'll see more details in other mail, but, here I am firing up standard python interpreter from within windows terminal, and then executing following line: from scoping2 import * And, this is under windows 11 windows terminal, which is where I generally interact with my python code, via com

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Jacob Kruger via Python-list
Matt, other mail is more relevant - seems to maybe have more to do with different behavour if import code, or not - no, does not make sense to me - but, here's the command line contents including printing out id() results, but, only working via importing code: #---start session--- C:\temp\py_

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Jacob Kruger via Python-list
Thanks for all your input people, and, yes, I know that besides the scope oddities the rest of the code is not my normal style either - was partly due to forms of experimentation to try figure out what could be causing issues. For example, instead of [:] syntax, was specifically using copy() to

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Roel Schroeven via Python-list
Op 6/03/2024 om 16:39 schreef Roel Schroeven via Python-list: Op 6/03/2024 om 13:55 schreef Jacob Kruger via Python-list: If you import the contents of that file into the python interpreter, [...] What exactly to you mean by "import the contents of that file into the python interpreter"? Othe

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Roel Schroeven via Python-list
Op 6/03/2024 om 13:55 schreef Jacob Kruger via Python-list: If you import the contents of that file into the python interpreter, [...] What exactly to you mean by "import the contents of that file into the python interpreter"? Other people have put your code in a script, executed it, and saw

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Thomas Passin via Python-list
On 3/6/2024 7:55 AM, Jacob Kruger via Python-list wrote: Ok, simpler version - all the code in a simpler test file, and working with two separate variables to explain exactly what am talking about: # start code from datetime import datetime, timezone, timedelta from copy import copy # initi

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Thomas Passin via Python-list
On 3/6/2024 5:59 AM, Alan Gauld via Python-list wrote: On 05/03/2024 22:46, Grant Edwards via Python-list wrote: Unfortunately (presumably thanks to SEO) the enshittification of Google has reached the point where searching for info on things like Python name scope, the first page of links are to

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Mats Wichmann via Python-list
On 3/6/24 05:55, Jacob Kruger via Python-list wrote: Ok, simpler version - all the code in a simpler test file, and working with two separate variables to explain exactly what am talking about: If you import the contents of that file into the python interpreter, dt_expiry will start off as "19

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Jacob Kruger via Python-list
So, this does not make sense to me in terms of the following snippet from the official python docs page: https://docs.python.org/3/faq/programming.html "In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Jacob Kruger via Python-list
Ok, simpler version - all the code in a simpler test file, and working with two separate variables to explain exactly what am talking about: # start code from datetime import datetime, timezone, timedelta from copy import copy # initialise original values dt_expiry = datetime.strptime("1970

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-06 Thread Alan Gauld via Python-list
On 05/03/2024 22:46, Grant Edwards via Python-list wrote: > Unfortunately (presumably thanks to SEO) the enshittification of > Google has reached the point where searching for info on things like > Python name scope, the first page of links are to worthless sites like > geeksforgeeks. And not just

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-05 Thread Grant Edwards via Python-list
On 2024-03-05, Cameron Simpson via Python-list wrote: > Because there are no variable definitions in Python, when you write > a function Python does a static analysis of it to decide which > variables are local and which are not. If there's an assignment to a > variable, it is a local variable.

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-05 Thread Cameron Simpson via Python-list
On 05Mar2024 20:13, Jacob Kruger wrote: Now, what almost seems to be occurring, is that while just manipulating the contents of a referenced variable is fine in this context, the moment I try to reassign it, that's where the issue is occurring . Because there are no variable definitions in Py

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-05 Thread dn via Python-list
Jacob, Please reduce the problem to a small code-set which reproduces the problem. If we can reproduce same, then that tells us something. At the very least, we can experiment without having to expend amounts of time in a (likely faulty) bid to reproduce the same environment. Also, code is t

Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-05 Thread Jacob Kruger via Python-list
Hi there Working with python 3.11, and, issue that confused me for a little while, trying to figure out what was occurring - unless am completely confused, or missing something - was that, for example, when having pre-defined a variable, and then included it in the global statement inside a

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-28 Thread jfong
jf...@ms4.hinet.net於 2018年12月28日星期五 UTC+8下午4時04分07秒寫道: > eryk sun at 2018/12/27 UTC+8 PM 6:58:33 wrote: > > On 12/27/18, jf...@ms4.hinet.net wrote: > > > > > > I still don't get it. When I change it to using list comprehension, the > > > problem is still there. (it now has no late-binding variable

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-28 Thread jfong
eryk sun at 2018/12/27 UTC+8 PM 6:58:33 wrote: > On 12/27/18, jf...@ms4.hinet.net wrote: > > > > I still don't get it. When I change it to using list comprehension, the > > problem is still there. (it now has no late-binding variable, right? :-) > > > class Too: > > ... XS = [15, 15, 15,

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Chris Angelico
On Fri, Dec 28, 2018 at 8:47 AM eryk sun wrote: > > On 12/27/18, Chris Angelico wrote: > > > > Class scope is special, and a generator expression within that class > > scope is special too. There have been proposals to make these kinds of > > things less special, but the most important thing to r

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread eryk sun
On 12/27/18, Chris Angelico wrote: > > Class scope is special, and a generator expression within that class > scope is special too. There have been proposals to make these kinds of > things less special, but the most important thing to remember is that > when you create a generator expression, it

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Chris Angelico
On Fri, Dec 28, 2018 at 3:31 AM Ian Kelly wrote: > > On Wed, Dec 26, 2018 at 11:21 PM Chris Angelico wrote: > > > > On Thu, Dec 27, 2018 at 1:56 PM wrote: > > > > > > I saw the code below at stackoverflow. I have a little idea about the > > > scope of a class, and list comprehension and generat

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Ian Kelly
On Wed, Dec 26, 2018 at 11:21 PM Chris Angelico wrote: > > On Thu, Dec 27, 2018 at 1:56 PM wrote: > > > > I saw the code below at stackoverflow. I have a little idea about the scope > > of a class, and list comprehension and generator expressions, but still > > can't figure out why Z4 works and

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread eryk sun
On 12/27/18, jf...@ms4.hinet.net wrote: > > I still don't get it. When I change it to using list comprehension, the > problem is still there. (it now has no late-binding variable, right? :-) > class Too: > ... XS = [15, 15, 15, 15] > ... Z4 = [val for val in XS] > ... Z5 = [XS[0]

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Terry Reedy
On 12/26/2018 9:53 PM, jf...@ms4.hinet.net wrote: I saw the code below at stackoverflow. I have a little idea about the scope of a class, and list comprehension and generator expressions, but still can't figure out why Z4 works and Z5 not. Can someone explain it? (in a not-too-complicated way:

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread jfong
eryk sun於 2018年12月27日星期四 UTC+8下午2時31分58秒寫道: > On 12/26/18, jf...@ms4.hinet.net wrote: > > I saw the code below at stackoverflow. I have a little idea about the scope > > of a class, and list comprehension and generator expressions, but still > > can't figure out why Z4 works and Z5 not. Can someon

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread eryk sun
On 12/26/18, jf...@ms4.hinet.net wrote: > I saw the code below at stackoverflow. I have a little idea about the scope > of a class, and list comprehension and generator expressions, but still > can't figure out why Z4 works and Z5 not. Can someone explain it? (in a > not-too-complicated way:-) > >

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread Chris Angelico
On Thu, Dec 27, 2018 at 1:56 PM wrote: > > I saw the code below at stackoverflow. I have a little idea about the scope > of a class, and list comprehension and generator expressions, but still can't > figure out why Z4 works and Z5 not. Can someone explain it? (in a > not-too-complicated way:-)

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread Abdur-Rahmaan Janhangeer
greetings, 1) Z4 = sum(val for val in XS) is same as Z4 = sum(XS) 2) class Foo() can also ne written as class Foo: 3) in Foo.x you are using the class just to assoxiate some variables with a name. what is the purpose of tge script / what are you trying to do? Abdur-Rahmaan Janhangeer htt

Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread jfong
I saw the code below at stackoverflow. I have a little idea about the scope of a class, and list comprehension and generator expressions, but still can't figure out why Z4 works and Z5 not. Can someone explain it? (in a not-too-complicated way:-) class Foo(): XS = [15, 15, 15, 15] Z4 =

Re: variable scope in try ... EXCEPT block.

2018-07-13 Thread Chris Angelico
On Fri, Jul 13, 2018 at 10:43 PM, Ed Kellett wrote: > On 2018-07-12 18:00, Chris Angelico wrote: >> What do you mean by "fix"? Make the 'x' bind eagerly? That would break >> basically every other use of closures. > > No. I mean make each x a new variable--closures would work as before, > for-loops

Re: variable scope in try ... EXCEPT block.

2018-07-13 Thread Ed Kellett
On 2018-07-12 18:00, Chris Angelico wrote: > What do you mean by "fix"? Make the 'x' bind eagerly? That would break > basically every other use of closures. No. I mean make each x a new variable--closures would work as before, for-loops would change. If we have subscopes, it seems natural that ent

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Chris Angelico
On Fri, Jul 13, 2018 at 2:44 PM, wrote: > On Thursday, July 12, 2018 at 7:16:48 PM UTC-4, Chris Angelico wrote: >> Not sure, but here's a simpler implementation: >> >> except Exception as .err.0: >> print(.err.0) >> .err.0 = None >> del .err.0 >> >> In other words, exactly the same as

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread codewizard
On Thursday, July 12, 2018 at 7:16:48 PM UTC-4, Chris Angelico wrote: > On Fri, Jul 13, 2018 at 8:10 AM Igor wrote: > > On Thursday, July 12, 2018 at 5:45:52 AM UTC-4, Ben Bacarisse wrote: > >> aleiphoenix writes: > >> > >> [snip] > >> > >> When an exception has been assigned using as target, it

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread aleiphoenix
On Thursday, July 12, 2018 at 5:45:52 PM UTC+8, Ben Bacarisse wrote: > > Yes, it's intentional, but it's not exactly a scope. In > > https://docs.python.org/3/reference/compound_stmts.html#try > > -- > Ben. Thank you for the reply. Never thought of this kind of problem in Python3. On Thurs

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Chris Angelico
On Fri, Jul 13, 2018 at 8:10 AM, wrote: > On Thursday, July 12, 2018 at 5:45:52 AM UTC-4, Ben Bacarisse wrote: >> aleiphoenix writes: >> >> [snip] >> >> When an exception has been assigned using as target, it is cleared at >> the end of the except clause. This is as if >> >> except E as N:

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Ben Bacarisse
Just word on quoting... codewiz...@gmail.com writes: > On Thursday, July 12, 2018 at 5:45:52 AM UTC-4, Ben Bacarisse wrote: >> >> [snip] You cut everything I wrote. What you left is what I quoted from the Python documentation. In fairness to the authors you should probably have cut the attrib

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread codewizard
On Thursday, July 12, 2018 at 5:45:52 AM UTC-4, Ben Bacarisse wrote: > aleiphoenix writes: > > [snip] > > When an exception has been assigned using as target, it is cleared at > the end of the except clause. This is as if > > except E as N: > foo > > was translated to > > excep

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Chris Angelico
On Thu, Jul 12, 2018 at 11:23 PM, Ed Kellett wrote: > Could we fix: > > for x in something: > blah(lambda a: a + x) > > while we're at it? What do you mean by "fix"? Make the 'x' bind eagerly? That would break basically every other use of closures. ChrisA -- https://mail.python.org/mailma

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Ed Kellett
On 2018-07-12 14:03, Chris Angelico wrote: > Dealing with reference cycles is generally done *periodically* rather > than immediately (CPython disposes of unreferenced objects immediately > upon last deref). You can avoid having a dedicated cycle detection > pass by using a mark-and-sweep GC, but t

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Chris Angelico
On Thu, Jul 12, 2018 at 10:31 PM, Ed Kellett wrote: > On 2018-07-12 10:59, Steven D'Aprano wrote: >> On Thu, 12 Jul 2018 01:37:24 -0700, aleiphoenix wrote: >> >>> My question is, does except ... as ... create a new scope from outer >>> block, causing 'err' be hidden from outer scope? Is this inten

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Ed Kellett
On 2018-07-12 10:59, Steven D'Aprano wrote: > On Thu, 12 Jul 2018 01:37:24 -0700, aleiphoenix wrote: > >> My question is, does except ... as ... create a new scope from outer >> block, causing 'err' be hidden from outer scope? Is this intentional? > > No, it is not a new scope, and yes, it is int

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Steven D'Aprano
On Thu, 12 Jul 2018 01:37:24 -0700, aleiphoenix wrote: > My question is, does except ... as ... create a new scope from outer > block, causing 'err' be hidden from outer scope? Is this intentional? No, it is not a new scope, and yes, it is intentional. It's a nasty hack, but a *necessary* nasty

Re: variable scope in try ... EXCEPT block.

2018-07-12 Thread Ben Bacarisse
aleiphoenix writes: > suppose following code running with Python-3.4.8 and Python-3.6.5 > > > # -*- coding: utf-8 -*- > > > def hello(): > err = None > print('0: {}'.format(locals())) > try: > b = 2 > print('1: {}'.format(locals())) > raise ValueError() >

variable scope in try ... EXCEPT block.

2018-07-12 Thread aleiphoenix
suppose following code running with Python-3.4.8 and Python-3.6.5 # -*- coding: utf-8 -*- def hello(): err = None print('0: {}'.format(locals())) try: b = 2 print('1: {}'.format(locals())) raise ValueError() print('2: {}'.format(locals())) except

Re: Variable scope in nested functions

2018-01-29 Thread Prahallad Achar
Thanks Chris, Without using nonlocal any other options available? On 30 Jan 2018 8:30 am, "Chris Angelico" wrote: > On Tue, Jan 30, 2018 at 1:48 PM, Prahallad Achar > wrote: > > def a() : > > Print (value) > > def b() : > > Value = 100 > > Return b > > > > Its a nested func

Re: Variable scope in nested functions

2018-01-29 Thread Chris Angelico
On Tue, Jan 30, 2018 at 1:48 PM, Prahallad Achar wrote: > def a() : > Print (value) > def b() : > Value = 100 > Return b > > Its a nested function. How can I use variable value just one function > above the parent function. > This is possible in tcl.. Is it possible in Python

Variable scope in nested functions

2018-01-29 Thread Prahallad Achar
def a() : Print (value) def b() : Value = 100 Return b Its a nested function. How can I use variable value just one function above the parent function. This is possible in tcl.. Is it possible in Python too? -- https://mail.python.org/mailman/listinfo/python-list

Re: variable scope of class objects

2015-10-22 Thread Luca Menegotto
Maybe I've been too cryptic. I apologize. Il 22/10/2015 01:35, JonRob ha scritto: @Dennis, Thanks for your example. My structure is very similar. And that's ok. But you can also 'attach' the constants to a class, if it makes sense. For example, the same code of Dennis can be written as:

Re: variable scope of class objects

2015-10-22 Thread Erik
On 20/10/15 22:33, jon...@mail.python.org wrote: In your comment you mentioned that convention is to declare variables (and constants?) in the construction (__ini__). I would suggest that 'constants' are not 'declared' in the __init__ method body, but either as class variables or (see later)

Re: variable scope of class objects

2015-10-21 Thread Luca Menegotto
Il 20/10/2015 23:33, JonRob ha scritto: Hello Luca, I very much appreciated your comments. And I understand the importance of "doing something right" (i.e. convention). This leads me to another question. Because I am interfacing with an I2C sensor I have many register definations to includ

Re: variable scope of class objects

2015-10-21 Thread JonRob
@Dennis, Thanks for your example. My structure is very similar. Perhaps I was reading too much into Luca's below statement regarding declaring variables. Regards, JonRob Luca wrote... >Please, note that declaring a variable in the constructor is only a >convention: in Python you can a

What does it mean for Python to have “constants”? (was: variable scope of class objects)

2015-10-20 Thread Ben Finney
Dennis Lee Bieber writes: > (Python does not have anything that one might consider a true constant > -- other than the language defined singletons: None, and maybe by now > True and False). Python now deals with those by making the names keywords:: >>> True = object() File "", line 1

Re: variable scope of class objects

2015-10-20 Thread JonRob
Hello Luca, I very much appreciated your comments. And I understand the importance of "doing something right" (i.e. convention). This leads me to another question. Because I am interfacing with an I2C sensor I have many register definations to include (30 register addresses and 26 Variables

Re: variable scope of class objects

2015-10-20 Thread JonRob
Thanks to all who replied to my question. I received a lot of information and points of view that are very helpful. I realize some of you folks spent more that a few minutes. I really appreciate your time. Pardon me that i replied to random832's post and not the original but my original was l

Re: variable scope of class objects

2015-10-20 Thread Luca Menegotto
Il 20/10/2015 08:38, Nagy László Zsolt ha scritto: When you say "they have nothing to do", it is almost true but not 100%. I know it, but when it comes to eradicate an idea that comes directly from C++-like languages, you must be drastic. Nuances come after... -- Ciao! Luca -- https://mail.

Re: variable scope of class objects

2015-10-19 Thread Nagy László Zsolt
> These two statements make me think you come from C++ or something > similar. > > In Python you can declare variables at class level, but this > declaration must NOT be interpreted in the same manner of a similar > declaration in C++: they remain at the abstract level of a class, and > they have

Re: variable scope of class objects

2015-10-19 Thread Luca Menegotto
Il 19/10/2015 20:39, JonRob ha scritto: I (think) I understand that in the below case, the word self could be replaced with "BME280" to explicitly call out a variable. But even still I don't know how explicit call out effects the scope of a variable. These two statements make me think you com

Re: variable scope of class objects

2015-10-19 Thread Nagy László Zsolt
> My questions are: > What is the scope of class variables? In Python, you bind values (objects) to names. It is conceptually different from "setting the value of a variable". In Python, scope applies to names, not variables. When you say "class variable", what do you mean? This may help: A nam

Re: variable scope of class objects

2015-10-19 Thread Nagy László Zsolt
> > #!/usr/bin/python > # -- developed using Python 2.7.3 > > class BME280: Not strictly related to the question, but you probably want to use so called "new style classes" when developing a new program for Python version 2. In other words, use: class BME280(object): instead of class BME280:

Re: variable scope of class objects

2015-10-19 Thread Terry Reedy
On 10/19/2015 7:19 PM, sohcahto...@gmail.com wrote: Class variables are accessible without creating an instance of a class. Also, changing the value of a class variable affects ALL instances of that class. This is because the variable belongs to the class itself, not any of the instances of

Re: variable scope of class objects

2015-10-19 Thread sohcahtoa82
On Monday, October 19, 2015 at 11:39:59 AM UTC-7, JonRob wrote: > Hi, > > I've having trouble understanding the self concept as it applies to > variables. I think I understand how it affects methods. > > I haven't been able to fully grasp the scope of class variables and > the effect of the "sel

Re: variable scope of class objects

2015-10-19 Thread Random832
jon...@mail.python.org writes: > > The below pseudo code is distilled from my 1st attempt at a functional > Python program on the RasPi. > > My questions are: > What is the scope of class variables? You must access them as members of the class or an instance of the class. > does the self. prefix

variable scope of class objects

2015-10-19 Thread JonRob
Hi, I've having trouble understanding the self concept as it applies to variables. I think I understand how it affects methods. I haven't been able to fully grasp the scope of class variables and the effect of the "self" to the scope of the variable. I (think) I understand that in the below ca

Re: Creating a local variable scope.

2009-12-09 Thread rwwh
On Nov 30, 1:26 am, markolopa wrote: > I would be much happier with the smaller namespace. To fix the code > that you show I would impose > > s = None > while True: >      s = raw_input("enter something: ") >      if s not in ('q', 'quit', 'exit'): break > print s So you propose: if the variable

Re: Creating a local variable scope.

2009-12-03 Thread Ben Finney
Lie Ryan writes: > On 12/2/2009 2:56 PM, Ben Finney wrote: > > The ‘camelCase’ form is not conformant with PEP 8 at all (which makes me > > glad, since it's hideous). > > For some reason, every time I look at a unittest code, I was thinking > of Java... and not just because it's modeled after JUn

Re: Creating a local variable scope.

2009-12-03 Thread Lie Ryan
On 12/2/2009 2:56 PM, Ben Finney wrote: The ‘camelCase’ form is not conformant with PEP 8 at all (which makes me glad, since it's hideous). For some reason, every time I look at a unittest code, I was thinking of Java... and not just because it's modeled after JUnitTest. -- http://mail.python

Re: Creating a local variable scope.

2009-12-02 Thread r0g
Ben Finney wrote: > markolopa writes: > >> Hi Roger, >> > […] >>> Long, descriptive variable names all_in_lower_case >>> Function names all in CamelCase >>> Global names are in ALL CAPS >> yes, pep8 I guess. > > Not quite: it deviates from PEP 8 on function names, which should rather > be ‘lower

Re: Creating a local variable scope.

2009-12-01 Thread Ben Finney
markolopa writes: > Hi Roger, > […] > > Long, descriptive variable names all_in_lower_case > > Function names all in CamelCase > > Global names are in ALL CAPS > > yes, pep8 I guess. Not quite: it deviates from PEP 8 on function names, which should rather be ‘lower_case_words_separated_by_unders

Re: Tracing variable scope (local vs. global)

2009-12-01 Thread Terry Reedy
Manuel Graune wrote: Hello, consider the following piece of code: a=1 b=2 def foo(c): b=3 return a + b + c In this case, when calling "foo", "a" will take the global value, "b" will take the local value and "c" will take the value assigned when calling the function. Since I consider

Re: Tracing variable scope (local vs. global)

2009-12-01 Thread Diez B. Roggisch
Manuel Graune schrieb: Hello, consider the following piece of code: a=1 b=2 def foo(c): b=3 return a + b + c In this case, when calling "foo", "a" will take the global value, "b" will take the local value and "c" will take the value assigned when calling the function. Since I conside

Re: Tracing variable scope (local vs. global)

2009-12-01 Thread Lie Ryan
On 12/2/2009 9:02 AM, Manuel Graune wrote: Hello, consider the following piece of code: a=1 b=2 def foo(c): b=3 return a + b + c In this case, when calling "foo", "a" will take the global value, "b" will take the local value and "c" will take the value assigned when calling the fun

Re: Creating a local variable scope.

2009-12-01 Thread markolopa
Hi Roger, > > That is what I do regularly...8-> > > Well really dude, you need to stop doing that. It's not a language > problem, it's a memory problem (human not RAM!). You guessed quite correctly!...:-) I can't recognise my code sometimes after a single week. That's why I spend a lot of time cl

Re: Creating a local variable scope.

2009-12-01 Thread markolopa
Hi Dave, Since you feel like discussing my weird idea of Python reform :-) lets go... On 30 Nov, 11:29, Dave Angel wrote: > Somehow you seem to think there's some syntax for "creating" avariable.  In > fact, what's happening is you're binding/rebinding a name > to an object.  And if you forbid

Tracing variable scope (local vs. global)

2009-12-01 Thread Manuel Graune
Hello, consider the following piece of code: a=1 b=2 def foo(c): b=3 return a + b + c In this case, when calling "foo", "a" will take the global value, "b" will take the local value and "c" will take the value assigned when calling the function. Since I consider this behaviour a possi

Re: Creating a local variable scope.

2009-11-30 Thread r0g
markolopa wrote: > On Nov 30, 4:46 am, Dave Angel wrote: >> markolopa wrote: >> or >> whether you accidentally reused the same name without giving it a new >> value in the new loop. > > That is what I do regularly...8-> > Well really dude, you need to stop doing that. It's not a language p

Re: Creating a local variable scope.

2009-11-30 Thread Alf P. Steinbach
* Jean-Michel Pichavant: Steven D'Aprano wrote: On Mon, 30 Nov 2009 02:11:12 +0100, Alf P. Steinbach wrote: I think if one could somehow declare names as const (final, readonly, whatever) then that would cover the above plus much more. Having real constants is one feature that I miss.

Re: Creating a local variable scope.

2009-11-30 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Mon, 30 Nov 2009 02:11:12 +0100, Alf P. Steinbach wrote: I think if one could somehow declare names as const (final, readonly, whatever) then that would cover the above plus much more. Having real constants is one feature that I miss. Because Python doesn't

Re: Creating a local variable scope.

2009-11-30 Thread Steven D'Aprano
On Mon, 30 Nov 2009 02:11:12 +0100, Alf P. Steinbach wrote: > I think if one could somehow declare names as const (final, readonly, > whatever) then that would cover the above plus much more. Having real constants is one feature that I miss. Because Python doesn't have constants, I find I've los

Re: Creating a local variable scope.

2009-11-30 Thread Dave Angel
markolopa wrote: On Nov 30, 4:46 am, Dave Angel wrote: markolopa wrote: Antoher 15 minutes lost because of that Python "feature"... Is it only me??? Yep, I think so. Not very consoling but thanks anyway!...:- You're proposing a much more complex scoping rule, w

Re: Creating a local variable scope.

2009-11-30 Thread Lie Ryan
On 11/30/2009 8:13 PM, markolopa wrote: On Nov 30, 4:46 am, Dave Angel wrote: markolopa wrote: Antoher 15 minutes lost because of that Python "feature"... Is it only me??? Yep, I think so. Not very consoling but thanks anyway!...:- You're proposing a much more complex scoping rule,

Re: Creating a local variable scope.

2009-11-30 Thread markolopa
On Nov 30, 4:46 am, Dave Angel wrote: > markolopa wrote: > > Antoher 15 minutes lost because of that Python "feature"... Is it only > > me??? > > Yep, I think so. Not very consoling but thanks anyway!...:- > You're proposing a much more complex scoping rule, > where if a variable already exi

Re: Creating a local variable scope.

2009-11-30 Thread markolopa
Hi Steve! On Nov 30, 1:46 am, Steve Howell wrote: > I occasionally make the error you make, but I think the real problem > you are having is lack of attention to detail.  If name collisions are > a common problem for you, consider writing shorter methods Yes, this could be a solution, though I b

Re: Creating a local variable scope.

2009-11-29 Thread Terry Reedy
markolopa wrote: so "domain" should not exist in my namespace since I have no information associated to "domain" only to "self.domains". Python should allow me to write safe code! Leaving iteration names bound after loop exit is a feature. If you do not like it, explicitly unbind it. -- ht

Re: Creating a local variable scope.

2009-11-29 Thread Dave Angel
markolopa wrote: === arg_columns =] for domain in self.domains: i =elf.get_column_index(column_names, domain.name) col =olumn_elements[i] if len(col) !=en(val_column): ValueError('column %s has not the same size as the value column %s' % (column_names[

Re: Creating a local variable scope.

2009-11-29 Thread Alf P. Steinbach
* markolopa: On 18 Sep, 10:36, "markol...@gmail.com" wrote: On Sep 11, 7:36 pm, Johan Grönqvist wrote: I find several places in my code where I would like tohavea variable scope that is smaller than the enclosing function/class/module definition. This is one of the si

Re: Creating a local variable scope.

2009-11-29 Thread Steve Howell
On Nov 29, 4:26 pm, markolopa wrote: > Less than 3 hours have passed since my last post and got yet another > bug that could be prevented if Python had the functionality that other > languages have to destroy variables when a block ends. Here is the > code: > > = > > arg_columns = [] > for

Re: Creating a local variable scope.

2009-11-29 Thread markolopa
Hi Lie! On Nov 29, 11:11 pm, Lie Ryan wrote: > here is another bug you might have if python have an "even-more-local" > scope: > > while True: >      s = raw_input("enter something: ") >      if s not in ('q', 'quit', 'exit'): break > print s > > if the while block has become its own namespace; p

Re: Creating a local variable scope.

2009-11-29 Thread Lie Ryan
On 11/30/2009 8:12 AM, markolopa wrote: Hi, On 18 Sep, 10:36, "markol...@gmail.com" wrote: On Sep 11, 7:36 pm, Johan Grönqvist wrote: I find several places in my code where I would like tohavea variable scope that is smaller than the enclosing function/class/module definition. T

  1   2   >