Re: How should we use global variables correctly?

2019-08-23 Thread Joel Goldstick
On Fri, Aug 23, 2019 at 4:00 AM Windson Yang wrote: > > Thank you all. I agreed with Frank that > > > It would make sense to use the 'global' keyword if you have a module > with various functions, several of which refer to 'foo', but only one of > which changes the value of 'foo'. > > I also found

Re: How should we use global variables correctly?

2019-08-23 Thread Windson Yang
Thank you all. I agreed with Frank that > It would make sense to use the 'global' keyword if you have a module with various functions, several of which refer to 'foo', but only one of which changes the value of 'foo'. I also found an example in cpython/lib/gettext.py, only 'textdomain function' c

Re: How should we use global variables correctly?

2019-08-23 Thread Cameron Simpson
On 23Aug2019 09:07, Frank Millman wrote: On 2019-08-23 8:43 AM, Windson Yang wrote: In class.py class Example: def __init__(self): self.foo = 1 def bar() return self.foo + 1 Expect the syntax, why using class variable self.foo would be better (or m

Re: How should we use global variables correctly?

2019-08-23 Thread Cameron Simpson
On 23Aug2019 14:43, Windson Yang wrote: I also want to know what is the difference between "using 'global variables' in a py module" and "using a variable in class". For example: In global.py: foo = 1 def bar(): global foo return foo + 1 In class.py class Example:

Re: How should we use global variables correctly?

2019-08-23 Thread Frank Millman
On 2019-08-23 8:43 AM, Windson Yang wrote: I also want to know what is the difference between "using 'global variables' in a py module" and "using a variable in class". For example: In global.py: foo = 1 def bar(): global foo return foo + 1 In class.py class

Re: How should we use global variables correctly?

2019-08-22 Thread Windson Yang
I also want to know what is the difference between "using 'global variables' in a py module" and "using a variable in class". For example: In global.py: foo = 1 def bar(): global foo return foo + 1 In class.py class Example: def __init__(self): s

Re: How should we use global variables correctly?

2019-08-22 Thread Chris Angelico
On Fri, Aug 23, 2019 at 11:24 AM Windson Yang wrote: > > Thank you all for the great explanation, I still trying to find some good > example to use 'global', In CPython, I found an example use 'global' in > cpython/Lib/zipfile.py > > _crctable = None > def _gen_crc(crc): > for j in

Re: How should we use global variables correctly?

2019-08-22 Thread Windson Yang
Thank you all for the great explanation, I still trying to find some good example to use 'global', In CPython, I found an example use 'global' in cpython/Lib/zipfile.py _crctable = None def _gen_crc(crc): for j in range(8): if crc & 1: crc = (crc >> 1) ^

Re: How should we use global variables correctly?

2019-08-22 Thread Richard Damon
On 8/22/19 12:00 PM, Windson Yang wrote: > I can 'feel' that global variables are evil. I also read lots of articles > proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found > CPython Lib use quite a lot of `global` keyword. So how should we use > `global` keyword correctly? IIUC

Re: How should we use global variables correctly?

2019-08-22 Thread Chris Angelico
On Fri, Aug 23, 2019 at 10:18 AM Cameron Simpson wrote: > As Michael says, "you can always read from a parent scope if the name > hasn't been used by the local scope". What this means is this: > > _MODULE_LEVEL_CACHE = {} > > def factors_of(n): > factors = _MODULE_LEVEL_CACHE.get(n

Re: How should we use global variables correctly?

2019-08-22 Thread Cameron Simpson
On 22Aug2019 11:12, Michael Torrie wrote: On 8/22/19 10:00 AM, Windson Yang wrote: I can 'feel' that global variables are evil. I also read lots of articles proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found CPython Lib use quite a lot of `global` keyword. So how should w

Re: How should we use global variables correctly?

2019-08-22 Thread Michael Torrie
On 8/22/19 10:00 AM, Windson Yang wrote: > I can 'feel' that global variables are evil. I also read lots of articles > proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found > CPython Lib use quite a lot of `global` keyword. So how should we use > `global` keyword correctly? IIUC

How should we use global variables correctly?

2019-08-22 Thread Windson Yang
I can 'feel' that global variables are evil. I also read lots of articles proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found CPython Lib use quite a lot of `global` keyword. So how should we use `global` keyword correctly? IIUC, it's fine that we use `global` keyword inside t