python closure local variables

寵の児 提交于 2019-12-11 05:16:21

问题


In this answer a singleton decorator is demonstrated as such

def singleton(cls):
    instances = {}
    def getinstance():
        print len(instances)
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return getinstance

but instances is 'local' to each class that is decorated, so I tried to be more efficient and use

def BAD_singleton(cls):
    instances = None
    def getinstance():
        if instances is None:
            instances = cls()
        return instances
    return getinstance

@BAD_singleton
class MyTest(object):
    def __init__(self):
        print 'test'

However, this gives an error

UnboundLocalError: local variable 'instances' referenced before assignment

when m = MyTest() is called

I think I understand which this should not work (as the assignment to instances will be local and be lost between calls), but I do not understand why I am getting this error.


回答1:


The reason for the error is python is cleverer than I am and identified that instances is made local by the assignment and does not go up-scope to find the assignment. As pointed out in the comments by @GeeTransit this is possible in python3 via nonlocal

def nonlocal_singleton(cls):
    instances = None
    def getinstance():
        nonlocal instances
        if instances is None:
            instances = cls()
        return instances
    return getinstance

@nonlocal_singleton
class MyTest(object):
    def __init__(self):
        print('test')


来源:https://stackoverflow.com/questions/11142993/python-closure-local-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!