ple*_*oux 11 python class function twisted
我正在python和扭曲的框架中创建一个聊天守护进程.我想知道是否必须删除我的函数中创建的每个变量,以便在连接多个用户时长期保存内存,或者这些变量是否自动清除?这是我的代码的精简版,以说明我的观点:
class Chat(LineOnlyReceiver):
    LineOnlyReceiver.MAX_LENGTH = 500
    def lineReceived(self, data):
            self.sendMessage(data)
    def sendMessage(self, data):
            try:
                message = data.split(None,1)[1]
            except IndexError:
                return
            self.factory.sendAll(message)
            #QUESTION : do i have to delete message and date??????????????????
            del message
            del data
class ChatFactory(Factory):
    protocol = Chat
    def __init__(self):
        self.clients = []
    def addClient(self, newclient):
        self.clients.append(newclient)
    def delClient(self, client):
        self.clients.remove(client)
    def sendAll(self, message):
        for client in self.clients:
            client.transport.write(message + "\n")
J S*_*J S 16
C Python(参考实现)使用引用计数和垃圾收集.当对象的引用计数减少到0时,它会自动回收.垃圾收集通常仅回收那些彼此引用的对象(或来自它们的其他对象),因此无法通过引用计数回收.
因此,在大多数情况下,在函数结束时回收局部变量,因为在函数退出时,对象停止从任何地方引用.所以你的"del"语句是完全没必要的,因为无论如何Python都会这样做.