Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python内部函数和id_Python_Function_Closures_Variable Assignment - Fatal编程技术网

python内部函数和id

python内部函数和id,python,function,closures,variable-assignment,Python,Function,Closures,Variable Assignment,在实验python装饰器时,我得到了一个超出我理解的结果,与内部函数、闭包、赋值相关 我尝试下面的代码 def myfunc(): print("myfunc") def decorator1(func): def inner1(*args, **kwargs): func(*args, **kwargs) print("inner1 id = %d " % id(inner1)) # print(inner1.cache) # retu

在实验python装饰器时,我得到了一个超出我理解的结果,与内部函数、闭包、赋值相关

我尝试下面的代码

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()
并低于输出

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
我的问题是

1为什么在Block1中,两个decorator2为inner2调用相同的id,但是 inner2.cache的不同id

2为什么在Block2中,inner2 id开始更改,而在Block1中没有更改?(块2指定返回值,块1不指定。)


Python在重新分配变量时,有时会因为CPython优化而更改其id,例如:

def f():
  return None 

$> f.a = {}
$> id(f.a)
$> 140511869957216
$> f.a = {}
$> id(f.a)
$> 140511869504400
你可以检查这个问题,也许可以帮助你澄清

这真的很好,我将强调它给出的一些例子

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

这里发生了什么事?即使将相同的整数值分配给不同的变量名,我们也会得到两个不同的ID。这些实际上是我们在这里观察到的CPython优化的效果。CPython实现为-5到256之间的所有整数保留一个整数对象数组。因此,当我们在该范围内创建一个整数时,它们只是返回对现有对象的引用。有关详细信息,请参阅以下链接。

Python在重新分配变量时,有时会因为CPython优化而更改其id,例如:

def f():
  return None 

$> f.a = {}
$> id(f.a)
$> 140511869957216
$> f.a = {}
$> id(f.a)
$> 140511869504400
你可以检查这个问题,也许可以帮助你澄清

这真的很好,我将强调它给出的一些例子

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

这里发生了什么事?即使将相同的整数值分配给不同的变量名,我们也会得到两个不同的ID。这些实际上是我们在这里观察到的CPython优化的效果。CPython实现为-5到256之间的所有整数保留一个整数对象数组。因此,当我们在该范围内创建一个整数时,它们只是返回对现有对象的引用。您可以参考以下链接了解更多信息。

Garbashe collector?也许吧,看。是的,这与垃圾收集有关,但也与CPython碰巧在同一地址分配新对象有关。不过,一般情况下不需要这样做。@SoraweePorncharoenwase Yesss的确,我只是发现有一篇帖子在谈论这一点,非常真实,非常有趣,我对它的收藏家Garbashe一无所知?也许吧,看。是的,这与垃圾收集有关,但也与CPython碰巧在同一地址分配新对象有关。不过,一般来说,这并不需要。@SoraweePorncharoenwase Yesss的确,我只是找到一篇关于这一点的帖子,非常真实,非常有趣,我对此一无所知