Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109827 > unrolled thread
| Started by | Marcin Rak <mrak@sightlineinnovation.com> |
|---|---|
| First post | 2016-06-11 16:44 -0700 |
| Last post | 2016-06-21 16:50 -0700 |
| Articles | 2 on this page of 42 — 12 participants |
Back to article view | Back to comp.lang.python
the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-11 16:44 -0700
Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-11 19:50 -0400
Re: the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-11 17:12 -0700
Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-11 21:27 -0400
Re: the global keyword: Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-11 20:15 -0700
Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-12 14:46 -0400
Re: the global keyword: MRAB <python@mrabarnett.plus.com> - 2016-06-12 01:09 +0100
Re: the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-11 17:13 -0700
Re: the global keyword: Ned Batchelder <ned@nedbatchelder.com> - 2016-06-11 20:41 -0700
Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-11 21:26 -0400
Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-12 13:38 +1000
Re: the global keyword: Ned Batchelder <ned@nedbatchelder.com> - 2016-06-11 20:48 -0700
Re: the global keyword: Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-12 11:31 -0700
Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-12 20:07 +0100
Re: the global keyword: Ned Batchelder <ned@nedbatchelder.com> - 2016-06-12 12:25 -0700
Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-12 22:10 +0100
Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-19 16:35 +0200
Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-19 22:20 +0100
Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-20 13:14 +0200
Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-20 22:15 +1000
Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-20 15:04 +0200
Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-20 09:29 -0400
Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-21 00:53 +1000
Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-21 10:08 +0200
Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-21 11:41 +0100
Re: the global keyword: Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-21 13:01 +0200
Re: the global keyword: Joel Goldstick <joel.goldstick@gmail.com> - 2016-06-19 13:16 -0400
Re: the global keyword: Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-19 22:50 -0700
Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-20 11:21 +0100
Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-20 22:03 +1000
Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 10:04 -0700
Re: the global keyword: Random832 <random832@fastmail.com> - 2016-06-21 13:15 -0400
Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 15:20 -0700
Re: the global keyword: Chris Angelico <rosuav@gmail.com> - 2016-06-22 08:32 +1000
Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 16:43 -0700
Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-22 00:15 +0100
Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 17:24 -0700
Re: the global keyword: BartC <bc@freeuk.com> - 2016-06-22 10:24 +0100
Re: the global keyword: Steven D'Aprano <steve@pearwood.info> - 2016-06-24 11:00 +1000
Re: the global keyword: Chris Angelico <rosuav@gmail.com> - 2016-06-24 11:19 +1000
Re: the global keyword: eryk sun <eryksun@gmail.com> - 2016-06-24 02:19 +0000
Re: the global keyword: Rick Johnson <rantingrickjohnson@gmail.com> - 2016-06-21 16:50 -0700
Page 3 of 3 — ← Prev page 1 2 [3]
| From | eryk sun <eryksun@gmail.com> |
|---|---|
| Date | 2016-06-24 02:19 +0000 |
| Message-ID | <mailman.89.1466734840.11516.python-list@python.org> |
| In reply to | #110445 |
On Fri, Jun 24, 2016 at 1:00 AM, Steven D'Aprano <steve@pearwood.info> 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:
>>> import builtins
>>> exec(r'''
... foo = 'bar'
... ''', vars(builtins))
>>> builtins.foo
'bar'
The frame for an unoptimized CPython code object (used for modules and
the class statement) uses f_builtins, f_globals, and f_locals dicts
(or possibly a mapping in the case of f_locals) respectively for its
builtin, global, and local scopes. The frame for an optimized code
object uses a fast locals array instead of f_locals, in which case the
locals() function stores a snapshot of the locals to the f_locals
dict. A frame may also have a freevars array, which has cell objects
for closures (i.e. the nonlocal scope). For optimized code (i.e. a
function, but not a class body), locals() also includes a snapshot of
nonlocals, even though this doesn't make literal sense.
The above example executes code with all 3 scopes set to the same
dict. For example:
>>> exec(r'''
... import sys
... f = sys._getframe()
... print(f.f_builtins is f.f_globals is f.f_locals)
... ''', vars(builtins))
True
The CPython VM doesn't have an operation to store to the builtin
scope, since the language doesn't have a statement such as "builtin"
to declare a variable in this scope. The builtin scope is used as a
fallback for operations such as LOAD_NAME and LOAD_GLOBAL -- for
loading constants such as built-in functions, exceptions, __debug__,
and also the "_" attribute that gets updated by sys.displayhook.
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2016-06-21 16:50 -0700 |
| Message-ID | <73124fac-0993-4194-8658-4f3b661d52dc@googlegroups.com> |
| In reply to | #110270 |
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'
> py> sys.modules['__builtin__'].__dict__['foo_func']
> <function foo at 0x02810330>
Oops. Made a innocent boo-boo here. Let me try again.
py> import sys
py> def foo():
... print "Hey, my name is foo!"
py> sys.modules['__builtin__'].__dict__['foo_func'] = foo
py> foo_func()
Hey, my name is foo!
py> foo_func = 'bar'
py> foo_func
'bar'
py> sys.modules['__builtin__'].__dict__['foo_func']
<function foo at 0x026C0330>
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.python
csiph-web