如何在python中卸载类的实例

时间:2016-09-01 02:43:11

标签: python class

我认为最好举例说明我为什么要这样做。 说我有一场比赛。 有一些对象是enemeys 这些enemeys有一个正在运行的类(健康,武器,等) 如果一个玩家杀死一个敌人我想从游戏中卸载那个enemeys数据(删除该类的enemys实例)我该如何去做呢?

编辑:

好的使用del剂量没有帮助。这是一个基本的例子,想象力obj1是一个敌人,并且在i = 10想象之后敌人死亡。 我有删除obj的代码但是obj1中的函数Update()仍然被调用,因为shell仍然打印“AAAAAAAAAAAA”,即使shell将d打印为True

import threading
import time
import sched
#import Queue

#setting up some OneInstance Var's
starttime=time.time()
condition = threading.Condition()
lock = threading.Lock()

#This is the tick method for calling the update threads in the child classes (set at 20 Ticks/s)
#The tick is run in its own thread so it can not be interrupted
#The tick method is part of the Base module but not part of the base class As there needs to be ONLEY ONE INSTANCE of it
def Tick(cond):
    while True:
        with cond:
            cond.notifyAll() #This clears the block on the threads evey 20th of a second
        time.sleep(0.05 - ((time.time() - starttime) % 0.05)) #This is the 20th of a second part

tickThread=threading.Thread(name = 'ThreadTick', target=Tick, args=(condition,)) #This setsup the Tick in its own thread
tickThread.start()#This runs said thread

#BaseClass is the class All other classes inhearit from
class BaseClass(object):
    def __init__(self, name):
        global condition
        global lock
        self.name = name
        t=threading.Thread(name = 'Thread'+self.name, target=self.UpdateHandler, args=(condition,lock,))
        t.start()


    def BaseClassType(self):
        """Returns a string of the childs type"""
        pass

    def UpdateHandler(self, cond, lock):
        #this part handles when to call the update.
        #it is allso needed so that it can be run in a thread.
        while True:
            self.lock = lock #this is just passed down evey tick so that threads can lock them selfs when writing to the shell
            self.Update() #Calls the update on all child classes
            with cond:
                cond.wait()#This makes all the threads waite(besides the tick thread) when they are done for the next tick.

    def Update(self):
        #This is a place holder.
        #It stops classes that don't have a Update function crashing when called
        pass

    def Unload(self):
        #this method will terminate the thread.... Don't know if this is even possible in python yet
        #and then remove any active instances of itself befor removing the class instance
        pass

    #This class is made so that I did not have to rewrite self.lock.blablabla eatch time i wanted to print to the shell
    def Print (self, message):
        self.lock.acquire()
        print(message)
        self.lock.release()

#---------------------------------------------------------------------------------
class ChildClassA(BaseClass):
    def __init__(self, name):
        super(ChildClassA, self).__init__(name)

    def BaseClassType(self):
        return 'ChildClassA'

    def Update(self):
        self.Print('AAAAAAAAAAAAAAAAAAAAAAA')

#----------------------------------------------------------------------------------
class ChildClassB(BaseClass):
    def __init__(self, name):
        super(ChildClassB, self).__init__(name)

    def Update(self):
        #print(self.name, "This is a completley different action")

        self.Print('BBBBBBBBBBBBBBBBBBB')
        self.Hi()

    def Hi(self):
        self.Print("Hi")
#----------------------------------------------------------------------------------
#works now just like in unity
class ChildClassC(BaseClass): #the new class
    def __init__(self, name): #this is the equivalent of start()
        super(ChildClassC, self).__init__(name) #this is the onley extra bit but its just to pass some name data through the classes

    def Update(self): #this is litaley the same as update() except it has self in the parameters

        self.Print("CCCCCCCCCCCCCCCCCCCCCCCCCC")

#--------------------------------------------------------------------------------

obj1 = ChildClassA('Obj1') 
obj2 = ChildClassB('Obj2')
obj3 = ChildClassC('Obj3')

i=0
d=False
while True:
    if i >=10:
        if d==False:
            del obj1
            d = True
    i=i+1
    print("D: ", d)
    print("I: ", i)

1 个答案:

答案 0 :(得分:1)

目前尚不清楚你在问什么。但我猜它是以下之一:

  • 您来自C背景,其中对象被分配和释放,Python是您的第一个垃圾收集语言。如果删除对它的所有引用(例如从精灵列表中删除它或诸如此类的东西)。垃圾收集会自动将其从内存中删除。
  • 如果您询问它是如何从游戏场景中删除的,那么您必须更具体地了解您正在使用的游戏框架或其他现有代码。