Re: the global keyword:

2016-06-23 Thread eryk sun
On Fri, Jun 24, 2016 at 1:00 AM, Steven D'Aprano wrote: > To be precise, for the builtins module specifically, you can *read* the > value of the variable using just x, but you cannot *assign* to it unless > you use the fully-qualified name builtins.x. FWIW, you can execute code in any dict:

Re: the global keyword:

2016-06-23 Thread Chris Angelico
On Fri, Jun 24, 2016 at 11:00 AM, Steven D'Aprano wrote: > To me, a global variable is a variable which is scoped to a level wider than > any single function, i.e. module scope, or whole-application scope. That > is, the variable must be visible to more than one function, or more than > one module

Re: the global keyword:

2016-06-23 Thread Steven D'Aprano
On Wed, 22 Jun 2016 07:24 pm, BartC wrote: [...] > But even if it worked, I don't think this two-parter counts as a 'global > variable' as it is understood. I might as well just create my own G > module containing: > > BDFL="GvR" > > and import it in both A and B. Then I can also write G.BD

Re: the global keyword:

2016-06-22 Thread BartC
On 22/06/2016 01:24, Rick Johnson wrote: On Tuesday, June 21, 2016 at 6:16:09 PM UTC-5, BartC wrote: I tried using your method but it didn't work: ...you'll find a thread i authored, that includes an object exposing a global namespace named "G". Details of how to inject the symbol G are inc

Re: the global keyword:

2016-06-21 Thread Rick Johnson
On Tuesday, June 21, 2016 at 6:16:09 PM UTC-5, BartC wrote: > On 21/06/2016 23:20, Rick Johnson wrote: > > On Tuesday, June 21, 2016 at 12:15:38 PM UTC-5, Random832 wrote: > > > Storing dynamic data to global space is almost always > > foolish, and I'm a fan of name spaces. But i did not > > recom

Re: the global keyword:

2016-06-21 Thread Rick Johnson
On Tuesday, June 21, 2016 at 5:20:53 PM UTC-5, Rick Johnson wrote: > py> import sys > py> def foo(): > ... print 'My name is Foo!' > py> sys.modules['__builtin__'].__dict__['foo_func'] = foo > py> foo() > My name is Foo! > py> foo = 'bar' > py> foo > 'bar' >

Re: the global keyword:

2016-06-21 Thread Rick Johnson
On Tuesday, June 21, 2016 at 5:32:43 PM UTC-5, Chris Angelico wrote: > On Wed, Jun 22, 2016 at 8:20 AM, Rick Johnson: > > Then one could have added module-level symbols without all > > the semantic hubbub. > > > > MSFL.foo = 0 > > > > def iter_foo(): > > MSFL.foo += 1 > > > > And d

Re: the global keyword:

2016-06-21 Thread BartC
On 21/06/2016 23:20, Rick Johnson wrote: On Tuesday, June 21, 2016 at 12:15:38 PM UTC-5, Random832 wrote: Storing dynamic data to global space is almost always foolish, and I'm a fan of name spaces. But i did not recommend such foolish action, i was merely replying to the assertion that "Pytho

Re: the global keyword:

2016-06-21 Thread Chris Angelico
On Wed, Jun 22, 2016 at 8:20 AM, Rick Johnson wrote: > Then one could have added module-level symbols without all > the semantic hubbub. > > MSFL.foo = 0 > > def iter_foo(): > MSFL.foo += 1 > And don't forget that you would need to call this function as MSFL.iter_foo(). Module-lev

Re: the global keyword:

2016-06-21 Thread Rick Johnson
On Tuesday, June 21, 2016 at 12:15:38 PM UTC-5, Random832 wrote: > You can put a function or constant there, sure. But if > you're using it as a variable, you'd have to do that > *every* time (in which case what's the point) Well, since the term "variable" has been so abused, using it in such a n

Re: the global keyword:

2016-06-21 Thread Random832
On Tue, Jun 21, 2016, at 13:04, Rick Johnson wrote: > On Sunday, June 12, 2016 at 2:08:01 PM UTC-5, BartC wrote: > > Anyway, it shows Python doesn't have true cross-module globals. > > BS! You can inject symbols into sys.modules and achieve a > true global. You can put a function or constant th

Re: the global keyword:

2016-06-21 Thread Rick Johnson
On Sunday, June 12, 2016 at 2:08:01 PM UTC-5, BartC wrote: > Anyway, it shows Python doesn't have true cross-module globals. BS! You can inject symbols into sys.modules and achieve a true global. ## BEGIN: INTERACTIVE SESSION ## py> import sys py> def foo(): ... print 'My name

Re: the global keyword:

2016-06-21 Thread Antoon Pardon
Op 21-06-16 om 12:41 schreef BartC: > On 21/06/2016 09:08, Antoon Pardon wrote: >> Op 20-06-16 om 16:53 schreef Steven D'Aprano: > >>> You know, there's not actually a rule or law that says you have to >>> automatically take the contrary position to everything I say. >> >> There is also not a rule

Re: the global keyword:

2016-06-21 Thread BartC
On 21/06/2016 09:08, Antoon Pardon wrote: Op 20-06-16 om 16:53 schreef Steven D'Aprano: You know, there's not actually a rule or law that says you have to automatically take the contrary position to everything I say. There is also not a rule of law that says you have to automatically introdu

Re: the global keyword:

2016-06-21 Thread Antoon Pardon
Op 20-06-16 om 16:53 schreef Steven D'Aprano: > On Mon, 20 Jun 2016 11:29 pm, Random832 wrote: > >> On Mon, Jun 20, 2016, at 08:15, Steven D'Aprano wrote: >>> Bart didn't say anyone had defended it. He made an observation: >>> >>> "that's a good illustration of why 'y' isn't a name reference to 'x'

Re: the global keyword:

2016-06-20 Thread Steven D'Aprano
On Mon, 20 Jun 2016 11:29 pm, Random832 wrote: > On Mon, Jun 20, 2016, at 08:15, Steven D'Aprano wrote: >> Bart didn't say anyone had defended it. He made an observation: >> >> "that's a good illustration of why 'y' isn't a name reference to 'x'" >> >> which is factually correct. And this does r

Re: the global keyword:

2016-06-20 Thread Random832
On Mon, Jun 20, 2016, at 08:15, Steven D'Aprano wrote: > Bart didn't say anyone had defended it. He made an observation: > > "that's a good illustration of why 'y' isn't a name reference to 'x'" > > which is factually correct. And this does refer to the "ducks limp" > thread. Except it doesn't.

Re: the global keyword:

2016-06-20 Thread Antoon Pardon
Op 20-06-16 om 14:15 schreef Steven D'Aprano: > On Mon, 20 Jun 2016 09:14 pm, Antoon Pardon wrote: > >> Op 19-06-16 om 23:20 schreef BartC: >>> On 19/06/2016 15:35, Antoon Pardon wrote: Op 12-06-16 om 23:10 schreef BartC: > On 12/06/2016 20:25, Ned Batchelder wrote: >> Just as here t

Re: the global keyword:

2016-06-20 Thread Steven D'Aprano
On Mon, 20 Jun 2016 09:14 pm, Antoon Pardon wrote: > Op 19-06-16 om 23:20 schreef BartC: >> On 19/06/2016 15:35, Antoon Pardon wrote: >>> Op 12-06-16 om 23:10 schreef BartC: On 12/06/2016 20:25, Ned Batchelder wrote: > Just as here there is no link between x > and y: > >

Re: the global keyword:

2016-06-20 Thread Steven D'Aprano
On Mon, 20 Jun 2016 08:21 pm, BartC wrote: > On 19/06/2016 18:16, Joel Goldstick wrote: > >> People who understand global variables generally avoid using them at >> all costs. > > Then perhaps they don't understand that in Python, top-level functions, > classes and imports are also globals. But

Re: the global keyword:

2016-06-20 Thread Antoon Pardon
Op 19-06-16 om 23:20 schreef BartC: > On 19/06/2016 15:35, Antoon Pardon wrote: >> Op 12-06-16 om 23:10 schreef BartC: >>> On 12/06/2016 20:25, Ned Batchelder wrote: Just as here there is no link between x and y: x = 12 y = x >>> >>> (And that's a good illustratio

Re: the global keyword:

2016-06-20 Thread BartC
On 19/06/2016 18:16, Joel Goldstick wrote: People who understand global variables generally avoid using them at all costs. Then perhaps they don't understand that in Python, top-level functions, classes and imports are also globals. -- Bartc -- https://mail.python.org/mailman/listinfo/pytho

Re: the global keyword:

2016-06-19 Thread Lawrence D’Oliveiro
On Monday, June 20, 2016 at 5:16:50 AM UTC+12, Joel Goldstick wrote: > People who understand global variables generally avoid using them at > all costs. I use them occasionally. > People who don't understand why globals create problems > seem to want to use them, and then become baffled at the pr

Re: the global keyword:

2016-06-19 Thread BartC
On 19/06/2016 15:35, Antoon Pardon wrote: Op 12-06-16 om 23:10 schreef BartC: On 12/06/2016 20:25, Ned Batchelder wrote: Just as here there is no link between x and y: x = 12 y = x (And that's a good illustration of why 'y' isn't a name reference to 'x', referring to the "...ducks

Re: the global keyword:

2016-06-19 Thread Joel Goldstick
On Sun, Jun 19, 2016 at 10:35 AM, Antoon Pardon wrote: > Op 12-06-16 om 23:10 schreef BartC: >> On 12/06/2016 20:25, Ned Batchelder wrote: >>> Just as here there is no link between x >>> and y: >>> >>> x = 12 >>> y = x >> >> (And that's a good illustration of why 'y' isn't a name referen

Re: the global keyword:

2016-06-19 Thread Antoon Pardon
Op 12-06-16 om 23:10 schreef BartC: > On 12/06/2016 20:25, Ned Batchelder wrote: >> Just as here there is no link between x >> and y: >> >> x = 12 >> y = x > > (And that's a good illustration of why 'y' isn't a name reference to 'x', > referring to the "...ducks limp" thread. But best n

Re: the global keyword:

2016-06-12 Thread BartC
On 12/06/2016 20:25, Ned Batchelder wrote: On Sunday, June 12, 2016 at 3:08:01 PM UTC-4, BartC wrote: On 12/06/2016 00:44, Marcin Rak wrote: from Test import some_function, my_print from Test import test_var some_function() my_print() print(test_var) *

Re: the global keyword:

2016-06-12 Thread Ned Batchelder
On Sunday, June 12, 2016 at 3:08:01 PM UTC-4, BartC wrote: > On 12/06/2016 00:44, Marcin Rak wrote: > > Hi to all. > > > > I have the following file named Solver.py: > > * > > from Test import some_function, my_print > > from Test import test_var > > > > some

Re: the global keyword:

2016-06-12 Thread BartC
On 12/06/2016 00:44, Marcin Rak wrote: Hi to all. I have the following file named Solver.py: * from Test import some_function, my_print from Test import test_var some_function() my_print() print(test_var) * and I h

Re: the global keyword:

2016-06-12 Thread Random832
On Sat, Jun 11, 2016, at 23:15, Lawrence D’Oliveiro wrote: > On Sunday, June 12, 2016 at 11:51:11 AM UTC+12, Random832 wrote: > > Importing a variable from a module copies its value into your own > > module's variable. > > Every name in Python is a variable, and can be assigned to to change its >

Re: the global keyword:

2016-06-12 Thread Random832
On Sat, Jun 11, 2016, at 23:15, Lawrence D’Oliveiro wrote: > On Sunday, June 12, 2016 at 11:51:11 AM UTC+12, Random832 wrote: > > Importing a variable from a module copies its value into your own > > module's variable. > > Every name in Python is a variable, and can be assigned to to change its >

Re: the global keyword:

2016-06-12 Thread Marcin Rak
Much thanks to all for their time, but Ned in particular...I learned something new about Python!! On Saturday, 11 June 2016 22:48:32 UTC-5, Ned Batchelder wrote: > On Saturday, June 11, 2016 at 11:38:33 PM UTC-4, Steven D'Aprano wrote: > > On Sun, 12 Jun 2016 11:26 am, Random832 wrote: > > > >

Re: the global keyword:

2016-06-12 Thread Lawrence D’Oliveiro
On Sunday, June 12, 2016 at 11:51:11 AM UTC+12, Random832 wrote: > Importing a variable from a module copies its value into your own > module's variable. Every name in Python is a variable, and can be assigned to to change its value at any time. -- https://mail.python.org/mailman/listinfo/python

Re: the global keyword:

2016-06-11 Thread Ned Batchelder
On Saturday, June 11, 2016 at 11:38:33 PM UTC-4, Steven D'Aprano wrote: > On Sun, 12 Jun 2016 11:26 am, Random832 wrote: > > > On Sat, Jun 11, 2016, at 20:09, MRAB wrote: > >> Not true. Importing doesn't copy the value. > >> > >> Importing a name creates a new name in the local scope that refers

Re: the global keyword:

2016-06-11 Thread Ned Batchelder
On Saturday, June 11, 2016 at 8:13:50 PM UTC-4, Marcin Rak wrote: > On Saturday, 11 June 2016 19:09:29 UTC-5, MRAB wrote: > > On 2016-06-12 00:50, Random832 wrote: > > > On Sat, Jun 11, 2016, at 19:44, Marcin Rak wrote: > > >> So my question is, how the heck is it possible that I get 5 as the last

Re: the global keyword:

2016-06-11 Thread Steven D'Aprano
On Sun, 12 Jun 2016 11:26 am, Random832 wrote: > On Sat, Jun 11, 2016, at 20:09, MRAB wrote: >> Not true. Importing doesn't copy the value. >> >> Importing a name creates a new name in the local scope that refers to >> the same object that the imported name referred to. MRAB is correct here. >

Re: the global keyword:

2016-06-11 Thread Random832
On Sat, Jun 11, 2016, at 20:12, Marcin Rak wrote: > What about variables that are user defined classes? Are they referenced > or copied? It will reference the same object, but if the variable is reassigned in the original module it will still not update the imported variable. -- https://mail.pyt

Re: the global keyword:

2016-06-11 Thread Random832
On Sat, Jun 11, 2016, at 20:09, MRAB wrote: > Not true. Importing doesn't copy the value. > > Importing a name creates a new name in the local scope that refers to > the same object that the imported name referred to. Yes, the value of a variable is a reference to an object. Can we not have anot

Re: the global keyword:

2016-06-11 Thread Marcin Rak
On Saturday, 11 June 2016 19:09:29 UTC-5, MRAB wrote: > On 2016-06-12 00:50, Random832 wrote: > > On Sat, Jun 11, 2016, at 19:44, Marcin Rak wrote: > >> So my question is, how the heck is it possible that I get 5 as the last > >> value printed? the global test_var (global to Test.py) I set to 44 w

Re: the global keyword:

2016-06-11 Thread Marcin Rak
On Saturday, 11 June 2016 18:51:11 UTC-5, Random832 wrote: > On Sat, Jun 11, 2016, at 19:44, Marcin Rak wrote: > > So my question is, how the heck is it possible that I get 5 as the last > > value printed? the global test_var (global to Test.py) I set to 44 when I > > ran some_function()??? does

Re: the global keyword:

2016-06-11 Thread MRAB
On 2016-06-12 00:50, Random832 wrote: On Sat, Jun 11, 2016, at 19:44, Marcin Rak wrote: So my question is, how the heck is it possible that I get 5 as the last value printed? the global test_var (global to Test.py) I set to 44 when I ran some_function()??? does anyone have a clue they could thr

Re: the global keyword:

2016-06-11 Thread Random832
On Sat, Jun 11, 2016, at 19:44, Marcin Rak wrote: > So my question is, how the heck is it possible that I get 5 as the last > value printed? the global test_var (global to Test.py) I set to 44 when I > ran some_function()??? does anyone have a clue they could throw my way? Importing a variable fr

the global keyword:

2016-06-11 Thread Marcin Rak
Hi to all. I have the following file named Solver.py: * from Test import some_function, my_print from Test import test_var some_function() my_print() print(test_var) * and I have the following Test.py: **