在 SO 中有几个关于 Python 垃圾收集的线程,在阅读了大约五个之后,加上一些在线文档,我仍然不确定垃圾收集是如何工作的,以及我应该如何管理我不使用的对象.事实上,在我读到的某处,有人不应该对收集垃圾做任何事情,其他人告诉人们应该 del
对象,而其他人再次解释说,取消引用一个对象足以让 Python 将其作为垃圾收集.
There are several threads on Python garbage collection in SO, and after reading about five, plus some doc on line, i am still not sure as to how garbage collection works and how i should manage objects which i am not using. In fact somewhere i read one should not do anything about collecting garbage, others tell one should del
objects, while others again explain de-referencing an object is enough for Python to collect it as garbage.
所以,冒着创建重复的风险,我会再次问这个问题,但不同的是,希望得到更全面和更清晰的信息.
So, at the risk of creating a duplicate, i will ask the question again, but differently, hoping to get more comprehensive and clearer information.
在我的例子中,我想用代表人的对象做一个小模拟.Person()
类的几个实例将被创建.它应该存在一段时间,直到它实际上死亡",而其他实例将被创建.
In my case i want to make a small simulation with objects representing people. Several instances of the Person()
class will be created. It should exist for some time until it virtually "dies" while other instances will be created.
现在我如何让这个 Person()
实例死掉"(假设将创建许多这样的实例,并且我不希望这些实例像幽灵一样闲逛)?
Now how do i make this Person()
instance "die" (assuming many many of these instances will be created and i don't want these instances to hang out like ghosts)?
有几种方法可以引用一个对象:
There are several ways i can reference an object:
john = Person('john')
或
people = []
people.append(Person('john'))
或
people = {}
people['john'] = Person('john')
保持我的程序干净、以最佳方式释放资源的最佳方法是什么?那么引用我的对象以便我可以控制对象的删除的最佳方法是什么?
What is the best way to keep my program clean, freeing resources optimally? And what is the best way then to reference my object so i can control the deletion of the object?
我发现大多数程序都非常自然地创建和处理对象,所以我通常从不担心.
I find that most programs create and dispose of objects quite naturally, so I never normally worry about it.
一些例子:
person = Person('john')
person = Person('james')
# Whoops! 'john' has died!
people = []
people.append(Person('john'))
# ...
# All 'Persons' live in people
people = []
# Now all 'Persons' are dead (including the list that referenced them)
class House():
def setOwner(self, person):
self.owner = person
house.setOwner(people[0])
# Now a House refers to a Person
people = []
# Now all 'Persons' are dead, except the one that house.owner refers to.
我假设你是这样的:
people = {}
people['john'] = Person('john')
def removePerson(personName):
del people[personName]
removePerson('john')
在这种情况下,people
是主列表,您可以控制何时从列表(它的字典)中添加和删除 Person
.
In this case people
is the master list and you can control when a Person
gets added and removed from the list (its a dictionary).
您可能必须非常彻底地思考一个人被创造然后死去的概念:一旦被创造出来,这个人如何首先与模拟互动.死后,你应该如何解开参考文献?(一个人可以引用其他东西,比如我的例子中的 House
可以让一个人活着.你可以让其他对象只保留这个人的名字).
You may have to think through the concept of a person being created and then dying very thoroughly: Once created how does the person first interact with the simulation. Upon death, how should you untangle the references? (Its ok for a person to refer to other stuff, its things like House
in my example that would keep a person alive. You could have other objects hold on to just the name of the person).
这篇关于如何将对象交给python垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!