This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2016年03月17日 07:40 by Ryan Fox, last changed 2022年04月11日 14:58 by admin.
| Messages (4) | |||
|---|---|---|---|
| msg261897 - (view) | Author: Ryan Fox (Ryan Fox) | Date: 2016年03月17日 07:40 | |
If a variable 'x' exists in the global or local scope, and a function (also defined in the same scope as 'x', or lower) refers only to a member named 'x' of an object, inspect.getclosurevars will include a reference to the variable, rather than the member.
Okay, that's kind of confusing to describe, so here's a small example in code form:
import inspect
class Foo:
x = int()
x = 1
f = Foo()
assert(f.x != x)
func = lambda: f.x == 0
assert(func())
cv = inspect.getclosurevars(func)
assert(cv.globals['f'] == f)
assert(cv.globals.get('x') != x) # <--- Assertion fails
It is expected that 'x' would not exist in cv.globals, since func does not refer to it. Also, there should be a 'f.x' included somewhere in the ClosureVariables object returned.
|
|||
| msg407082 - (view) | Author: Irit Katriel (iritkatriel) * (Python committer) | Date: 2021年11月26日 18:44 | |
Reproduced on 3.11. |
|||
| msg408856 - (view) | Author: hongweipeng (hongweipeng) * | Date: 2021年12月18日 15:34 | |
Why is expected that 'x' would not exist in cv.globals? I think it works normally, you can see `x` in `func.__globals__`. |
|||
| msg408866 - (view) | Author: Ryan Fox (Ryan Fox) | Date: 2021年12月18日 20:30 | |
If you change the class member 'x' to a different name like 'y', then cv doesn't include 'x', but does include an unbound 'y'. In both cases, the function isn't referring to a global variable, just the class member of that name. Besides the function itself, no other globals are included in cv.globals. On Sat, Dec 18, 2021 at 10:34 AM hongweipeng <report@bugs.python.org> wrote: > > hongweipeng <hongweichen8888@sina.com> added the comment: > > Why is expected that 'x' would not exist in cv.globals? I think it works > normally, you can see `x` in `func.__globals__`. > > ---------- > nosy: +hongweipeng > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue26577> > _______________________________________ > |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:58:28 | admin | set | github: 70764 |
| 2021年12月18日 20:30:07 | Ryan Fox | set | messages: + msg408866 |
| 2021年12月18日 15:34:16 | hongweipeng | set | nosy:
+ hongweipeng messages: + msg408856 |
| 2021年11月26日 18:44:24 | iritkatriel | set | nosy:
+ iritkatriel messages: + msg407082 |
| 2021年11月26日 18:37:43 | iritkatriel | set | versions: + Python 3.11, - Python 3.5, Python 3.6 |
| 2016年03月17日 07:50:06 | SilentGhost | set | nosy:
+ yselivanov type: behavior versions: - Python 3.3, Python 3.4 |
| 2016年03月17日 07:40:30 | Ryan Fox | create | |