简体   繁体   English

python内部函数和id

[英]python inner function and id

When experiment with python decorators, I got a result that are beyond my understanding, which is related with inner function, closure, assignment . 当尝试使用python装饰器时,我得到的结果超出了我的理解, 这与内部函数,闭包,赋值有关

I try below code, 我尝试下面的代码,

def myfunc():
    print("myfunc")

def decorator1(func):
    def inner1(*args, **kwargs):
        func(*args, **kwargs)
    print("inner1 id = %d " % id(inner1))
    # print(inner1.cache)
    # return inner1

def decorator2(func):
    def inner2(*args, **kwargs):
        func(*args, **kwargs)
    inner2.cache = {}
    print("inner2 id = %d " % id(inner2))
    print("\t\t\t cache id = %d " % id(inner2.cache))
    return inner2

# Block1 all same
decorator1(myfunc)
decorator1(myfunc)
decorator2(myfunc)
decorator2(myfunc)
decorator1(myfunc)
decorator1(myfunc)
print()

# Block2 deferrent when d2 = ...
decorator1(myfunc)
decorator1(myfunc)
d1 = decorator2(myfunc)
d2 = decorator2(myfunc)
decorator1(myfunc)
decorator1(myfunc)
print()

# Block3 all same
decorator1(myfunc)
decorator1(myfunc)
decorator2(myfunc)
decorator2(myfunc)
decorator1(myfunc)
decorator1(myfunc)
# print()

and get below output 并低于输出

inner1 id = 7696544290000
inner1 id = 7696544290000
inner2 id = 7696544290000
                        cache id = 7696550302496
inner2 id = 7696544290000
                        cache id = 7696547474720
inner1 id = 7696544290000
inner1 id = 7696544290000

inner1 id = 7696544290000
inner1 id = 7696544290000
inner2 id = 7696544290000
                        cache id = 7696550302496
inner2 id = 7696544291152
                        cache id = 7696547501392
inner1 id = 7696544290144
inner1 id = 7696544290144

inner1 id = 7696544290144
inner1 id = 7696544290144
inner2 id = 7696544290144
                        cache id = 7696548415040
inner2 id = 7696544290144
                        cache id = 7696547350000
inner1 id = 7696544290144
inner1 id = 7696544290144

my question is 我的问题是

1 why in Block1, two decorator2 call print same id for inner2, but different id for inner2.cache? 1为什么在Block1中,两个decorator2调用为inner2打印相同的id,但为inner2.cache打印不同的id?

2 why in Block2, inner2 id begin to change, where as in Block1 not? 2为什么在Block2中,inner2 id开始更改,而在Block1中没有变化? (Block2 assign the returned value, Block1 not.) (Block2分配返回的值,Block1不分配。)

Python, when you re assign a variable, changes its id sometimes because of CPython optimization, so, for example: 重新分配变量时,Python有时会由于CPython优化而更改其id,因此,例如:

def f():
  return None 

$> f.a = {}
$> id(f.a)
$> 140511869957216
$> f.a = {}
$> id(f.a)
$> 140511869504400

You can check this question how to re-assign variable in python without changing id? 您可以检查此问题, 如何在不更改id的情况下在python中重新分配变量? maybe can help you to clarify 也许可以帮助您澄清

This post is really nice to, I will highlight some examples it gave. 这篇文章非常好,我将重点介绍它提供的一些示例。

x = 500
y = 500
id(x)
4338740848
id(y)
4338741040

What happened here? 这里发生了什么? Even after assigning the same integer values to different variable names, we are getting two different ids here. 即使在将相同的整数值分配给不同的变量名称之后,我们在这里也会得到两个不同的id。 These are actually the effects of CPython optimization we are observing here. 这些实际上是我们在这里观察到的CPython优化的效果。 CPython implementation keeps an array of integer objects for all integers between -5 and 256. So when we create an integer in that range, they simply back reference to the existing object. CPython实现为-5到256之间的所有整数保留一个整数对象数组。因此,当我们在该范围内创建整数时,它们只是简单地引用现有对象。 You may refer the following links for more information. 您可以参考以下链接以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM