闭合:检索带有外部功能的变量
是否可以检索具有外部功能给出的变量?
Python:
def html_tag(tag):
print(" : <{t}>".format(t=tag))
def wrap_text(msg):
print("<{t}>{m}<{t}>".format(t=tag, m=msg))
return wrap_text
print_h1 = html_tag("h1")
print_div = html_tag("div")
print_span = html_tag("span")
print_h1("h1 - test met h1")
print_div("div - mouse button")
print_span("span - tekst met tag")
就我而言,我想在不同功能中查看/检索变量tag
。
print ( "help(print_h1) : ", help(print_h1), "")
print ( "dir(print_h1) : ", dir(print_h1), "")
print ( "print_h1.__dir__ : ", (print_h1.__dir__), "")
#print (print_h1.tag, "")
print ( "print_h1 : ",print_h1.__closure__, "\n")
print ( "print_h1 : ",print_h1, "\n")
返回:
Help on function wrap_text in module __main__:
wrap_text(msg)
help(print_h1) : None
dir(print_h1) : ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
print_h1.__dir__ : <built-in method __dir__ of function object at 0x7f2f42fbdb90>
print_h1 : (<cell at 0x7f2f42f73310: str object at 0x7f2f51f066b0>,)
print_h1 : <function html_tag.<locals>.wrap_text at 0x7f2f42fbdb90>
因此,当我像这样尝试时 它不在.__ dir __
...
print (print_h1.tag)
错误
AttributeError Traceback (most recent call last)
<ipython-input-55-71ea208bcc8c> in <module>()
----> 1 print (print_h1.tag)
AttributeError: 'function' object has no attribute 'tag'
编辑中 - 我想检索tag
变量
print_h1.__closure__: (<cell at 0x7f2f42f73310: str object at 0x7f2f51f066b0>,)
x
- 是从先前命令
import ctypes
x=0x7f2f51f066b0
# display memory address
print("Memory address - ", x)
# get the value through memory address
a = ctypes.cast(x, ctypes.py_object).value
# display
print("Value - ", a)
返回的手动检索的:
Memory address - 139841214899888
Value - h1
Is it possible to retrieve a variable, which is given with the outer function?
Python :
def html_tag(tag):
print(" : <{t}>".format(t=tag))
def wrap_text(msg):
print("<{t}>{m}<{t}>".format(t=tag, m=msg))
return wrap_text
print_h1 = html_tag("h1")
print_div = html_tag("div")
print_span = html_tag("span")
print_h1("h1 - test met h1")
print_div("div - mouse button")
print_span("span - tekst met tag")
In my case I would like to see/retrieve the variable tag
in the different functions.
print ( "help(print_h1) : ", help(print_h1), "")
print ( "dir(print_h1) : ", dir(print_h1), "")
print ( "print_h1.__dir__ : ", (print_h1.__dir__), "")
#print (print_h1.tag, "")
print ( "print_h1 : ",print_h1.__closure__, "\n")
print ( "print_h1 : ",print_h1, "\n")
returns :
Help on function wrap_text in module __main__:
wrap_text(msg)
help(print_h1) : None
dir(print_h1) : ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
print_h1.__dir__ : <built-in method __dir__ of function object at 0x7f2f42fbdb90>
print_h1 : (<cell at 0x7f2f42f73310: str object at 0x7f2f51f066b0>,)
print_h1 : <function html_tag.<locals>.wrap_text at 0x7f2f42fbdb90>
SO WHEN I TRY IT LIKE THIS
it is not in the dictionary of the .__dir__
...
print (print_h1.tag)
ERROR
AttributeError Traceback (most recent call last)
<ipython-input-55-71ea208bcc8c> in <module>()
----> 1 print (print_h1.tag)
AttributeError: 'function' object has no attribute 'tag'
EDIT - I would like to retrieve the tag
variable
print_h1.__closure__: (<cell at 0x7f2f42f73310: str object at 0x7f2f51f066b0>,)
address memory location : x
- is manual retrieved from previous command
import ctypes
x=0x7f2f51f066b0
# display memory address
print("Memory address - ", x)
# get the value through memory address
a = ctypes.cast(x, ctypes.py_object).value
# display
print("Value - ", a)
returns :
Memory address - 139841214899888
Value - h1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
__关闭__
是用户定义函数对象的(仅读取)属性:它是
cell
s的元组(或无),其中包含函数自由变量的绑定。单元格
对象具有(read&amp; write)属性cell_contents
。这可以用于获取/设置单元格的值。注意
单元格
可以通过 <代码>类型 模块代码
对象是 解释器使用的内部类型,用户可访问。co_freevars
是包含免费变量名称的元组,这里是如何获取函数参数及其标识符的示例:
__closure__
is an (read-only) attribute of a user-defined function object:it is tuple (or None) of
cell
s that contain bindings for the function’s free variables.A
cell
object has the (read & write) attributecell_contents
. This can be used to get/set the value of the cell.Note
cell
types can be accessed throughtypes
modulecode
objects are an internal type used by the interpreter and accessible by the user.co_freevars
is a tuple containing the names of free variableHere an example on how to get the value of the function's parameter and its identifier: