Re: exec and globals and locals ...

2019-09-20 Thread Peter Otten
jf...@ms4.hinet.net wrote:

 x = 3
 def foo():
> ... exec("print(globals(), locals()); x = x + 1; print(globals(),
> locals())") ...
 foo()
> {'foo': , '__package__': None, '__builtins__':
> {, '__loader__':  {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__':
> {'__main__', '__spec__': None, 'x': 3} {} 'foo':  {0x021C3468>, '__package__': None, '__builtins__':  {(built-in)>, '__loader__': ,
> {'__doc__': None, '__name__': '__main__', '__spec__': None, 'x': 3} {'x':
> {4}
 def goo():
> ... print(globals(), locals())
> ... x = x + 1
> ... print(globals(), locals())
> ...
 goo()
> {'foo': , '__package__': None, '__builtins__':
> {, '__loader__':  {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__':
> {'__main__', '__spec__': None, 'goo': , 'x':
> {3} {}
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "", line 3, in goo
> UnboundLocalError: local variable 'x' referenced before assignment

> 
> Can't figure out what makes it different:-(

(0) exec() and class definitions use the LOAD_NAME opcode which looks into 
the local namespace first and falls back to the global namespace. Therefore
x = x may look up a global x and assign to a local one

The function body uses either 

(1) if there is a name binding operation (assignment, augmented assignment, 
def, ...) LOAD_FAST which only looks into the local namespace. Thus
x = x will only succeed if x is already defined in the local namespace.

(2) if there is no binding operation (name appears in an expression, x[...] 
= ... or attribute assignment) LOAD_GLOBAL which only looks into the local 
namespace.

Your first example is case 0, your second is case 1.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exec and globals and locals ...

2019-09-20 Thread jfong
Peter Otten於 2019年9月20日星期五 UTC+8下午3時31分48秒寫道:
> jf...@ms4.hinet.net wrote:
> 
>  x = 3
>  def foo():
> > ... exec("print(globals(), locals()); x = x + 1; print(globals(),
> > locals())") ...
>  foo()
> > {'foo': , '__package__': None, '__builtins__':
> > {, '__loader__':  > {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__':
> > {'__main__', '__spec__': None, 'x': 3} {} 'foo':  > {0x021C3468>, '__package__': None, '__builtins__':  > {(built-in)>, '__loader__': ,
> > {'__doc__': None, '__name__': '__main__', '__spec__': None, 'x': 3} {'x':
> > {4}
>  def goo():
> > ... print(globals(), locals())
> > ... x = x + 1
> > ... print(globals(), locals())
> > ...
>  goo()
> > {'foo': , '__package__': None, '__builtins__':
> > {, '__loader__':  > {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__':
> > {'__main__', '__spec__': None, 'goo': , 'x':
> > {3} {}
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "", line 3, in goo
> > UnboundLocalError: local variable 'x' referenced before assignment
> 
> > 
> > Can't figure out what makes it different:-(
> 
> (0) exec() and class definitions use the LOAD_NAME opcode which looks into 
> the local namespace first and falls back to the global namespace. Therefore
> x = x may look up a global x and assign to a local one
> 
> The function body uses either 
> 
> (1) if there is a name binding operation (assignment, augmented assignment, 
> def, ...) LOAD_FAST which only looks into the local namespace. Thus
> x = x will only succeed if x is already defined in the local namespace.
> 
> (2) if there is no binding operation (name appears in an expression, x[...] 
> = ... or attribute assignment) LOAD_GLOBAL which only looks into the local 
> namespace.
> 
> Your first example is case 0, your second is case 1.

Hmm... exec() seems follow a more "LEGB" rule:-)

Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Won't Uninstall

2019-09-20 Thread Fred Vincent
Python 3.7.4 won’t uninstall, I have tried doing what I can such as going to 
the control panel and uninstalling the program there, but that did not work. 
Since I am unable to delete it I am unable to download a different version of 
python. How do I fix this and fully uninstall python?

Sent from Mail for Windows 10

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Won't Uninstall

2019-09-20 Thread MRAB

On 2019-09-20 03:59, Fred Vincent wrote:

Python 3.7.4 won’t uninstall, I have tried doing what I can such as going to 
the control panel and uninstalling the program there, but that did not work. 
Since I am unable to delete it I am unable to download a different version of 
python. How do I fix this and fully uninstall python?


You can have multiple versions of Python installed at the same time.

For testing purposes, I have Python 2.7 and from Python 3.5 to a release 
candidate of the upcoming Python 3.8, both 32-bit and 64-bit for all of 
them.

--
https://mail.python.org/mailman/listinfo/python-list


Re: regular expressions help

2019-09-20 Thread Barry Scott
When I'm debugging a regex I make the regex shorter and shorter to figure out
what the problem is.

Try starting with re.compile(r'm') and then add the chars one by one seeing
what happens as the string gets longer.

Barry


> On 19 Sep 2019, at 09:41, Pradeep Patra  wrote:
> 
> I am using python 2.7.6 but I also tried on python 3.7.3.
> 
> On Thursday, September 19, 2019, Pradeep Patra 
> wrote:
> 
>> Beginning of the string. But I tried removing that as well and it still
>> could not find it. When I tested at www.regex101.com and it matched
>> successfully whereas I may be wrong. Could you please help here?
>> 
>> On Thursday, September 19, 2019, David  wrote:
>> 
>>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra 
>>> wrote:
 
 pattern=re.compile(r'^my\-dog$')
 matches = re.search(mystr)
 
 In the above example both cases(match/not match) the matches returns
>>> "None"
>>> 
>>> Hi, do you know what the '^' character does in your pattern?
>>> 
>> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list