闭合:检索带有外部功能的变量

发布于 2025-02-05 14:13:24 字数 3107 浏览 2 评论 0原文

是否可以检索具有外部功能给出的变量?

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.

link to the excuted python code

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2025-02-12 14:13:24

__关闭__是用户定义函数对象的(仅读取)属性:
它是cell s的元组(或无),其中包含函数自由变量的绑定。

单元格对象具有(read&amp; write)属性cell_contents。这可以用于获取/设置单元格的值。

注意单元格可以通过 <代码>类型 模块


代码对象是 解释器使用的内部类型,用户可访问。

co_freevars是包含免费变量名称的元组


,这里是如何获取函数参数及其标识符的示例:

# html_tag: from the question

print_h1 = html_tag('h1')

# identifier of the parameter - with code obj
print(print_h1.__code__.co_freevars[0])
#tag

# value of the function's parameter - read
print(print_h1.__closure__[0].cell_contents)
#h1

# value of the function's parameter - write
print_h1.__closure__[0].cell_contents = 'h19'
print_h1('a')
#<h19>a<h19>

__closure__ is an (read-only) attribute of a user-defined function object:
it is tuple (or None) of cells that contain bindings for the function’s free variables.

A cell object has the (read & write) attribute cell_contents. This can be used to get/set the value of the cell.

Note cell types can be accessed through types module


code objects are an internal type used by the interpreter and accessible by the user.

co_freevars is a tuple containing the names of free variable


Here an example on how to get the value of the function's parameter and its identifier:

# html_tag: from the question

print_h1 = html_tag('h1')

# identifier of the parameter - with code obj
print(print_h1.__code__.co_freevars[0])
#tag

# value of the function's parameter - read
print(print_h1.__closure__[0].cell_contents)
#h1

# value of the function's parameter - write
print_h1.__closure__[0].cell_contents = 'h19'
print_h1('a')
#<h19>a<h19>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文