Re: The binding operator, and what gets bound to what (was: About Modifying Globals)

2014-12-05 Thread Chris Angelico
On Fri, Dec 5, 2014 at 11:26 PM, Steven D'Aprano wrote: > Chris Angelico wrote: > >>> def func(): >>> global math >>> import math >> >> When would you actually *want* this, though? Given that 'import' >> already caches, there's not much point caching globally, and the idea >> that a functi

Re: The binding operator, and what gets bound to what (was: About Modifying Globals)

2014-12-05 Thread Steven D'Aprano
Chris Angelico wrote: > On Fri, Dec 5, 2014 at 8:53 PM, Steven D'Aprano > wrote: >> Oh, I learned something new: strictly speaking, this is implementation- >> dependent and not guaranteed to work in the future! >> >> def func(): >> global math >> import math > > When would you actually *

Re: The binding operator, and what gets bound to what (was: About Modifying Globals)

2014-12-05 Thread Chris Angelico
On Fri, Dec 5, 2014 at 8:53 PM, Steven D'Aprano wrote: > Oh, I learned something new: strictly speaking, this is implementation- > dependent and not guaranteed to work in the future! > > def func(): > global math > import math When would you actually *want* this, though? Given that 'impor

Re: The binding operator, and what gets bound to what (was: About Modifying Globals)

2014-12-05 Thread Steven D'Aprano
Ben Finney wrote: > Steven D'Aprano writes: > >> LJ wrote: >> >> > def gt(l): >> >a["1"] = a["1"] | set([l]) >> >> The difference between this example and your second one: >> >> > def gt2(l): >> >b=b+l >> >> >> is that the second is a "binding operation" and the first is not. > > I disa

The binding operator, and what gets bound to what (was: About Modifying Globals)

2014-12-04 Thread Ben Finney
Steven D'Aprano writes: > LJ wrote: > > > def gt(l): > >a["1"] = a["1"] | set([l]) > > The difference between this example and your second one: > > > def gt2(l): > >b=b+l > > > is that the second is a "binding operation" and the first is not. I disagree; they're both binding operations (

Re: About Modifying Globals

2014-12-04 Thread shiyao.ma
On 12/04, LJ wrote: > Hi All, > > I have a quick question regarding the modification of global variables within > functions. To illustrate, consider the following toy example: > > a={"1": set()} > b=9 > > def gt(l): >a["1"] = a["1"] | set([l]) > > When calling this last function and checking t

Re: About Modifying Globals

2014-12-04 Thread Steven D'Aprano
LJ wrote: > Hi All, > > I have a quick question regarding the modification of global variables > within functions. To illustrate, consider the following toy example: > > a={"1": set()} > b=9 > > def gt(l): >a["1"] = a["1"] | set([l]) The difference between this example and your second one:

Re: About Modifying Globals

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 10:54 AM, Dave Angel wrote: > Python doesn't have declarations, so when a function is compiled, the > compiler has to infer what names are to be local and what are not. The rule > it normally uses is roughly based on whether an assignment occurs somewhere > inside the funct

Re: About Modifying Globals

2014-12-04 Thread Dave Angel
On 12/04/2014 03:09 PM, LJ wrote: Hi All, I have a quick question regarding the modification of global variables within functions. To illustrate, consider the following toy example: a={"1": set()} b=9 def gt(l): a["1"] = a["1"] | set([l]) When calling this last function and checking the

Re: About Modifying Globals

2014-12-04 Thread Ian Kelly
On Thu, Dec 4, 2014 at 1:09 PM, LJ wrote: > > Hi All, > > I have a quick question regarding the modification of global variables within functions. To illustrate, consider the following toy example: > > a={"1": set()} > b=9 > > def gt(l): >a["1"] = a["1"] | set([l]) > > When calling this last f

Re: About Modifying Globals

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 7:09 AM, LJ wrote: > def gt(l): >a["1"] = a["1"] | set([l]) > > > def gt2(l): >b=b+l These two may both look like they're assigning something, but one of them is assigning directly to the name "b", while the other assigns to a subscripted element of "a". In the firs

About Modifying Globals

2014-12-04 Thread LJ
Hi All, I have a quick question regarding the modification of global variables within functions. To illustrate, consider the following toy example: a={"1": set()} b=9 def gt(l): a["1"] = a["1"] | set([l]) When calling this last function and checking the a dictionary, I get: >>> gt(5) >>> a