如何将对象转移到python垃圾回收? [英] How to give object away to python garbage collection?

查看:115
本文介绍了如何将对象转移到python垃圾回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO中有几个关于Python垃圾收集的线程,在阅读了大约五篇文章以及一些文档后,我仍然不确定垃圾收集是如何工作的以及如何管理我不使用的对象。事实上,我读过的一个人不应该做任何关于收集垃圾的事情,其他人告诉我们应该 del 对象,而另外一些人解释了去引用对象足以让Python收集它作为垃圾。



因此,冒着创建重复的风险,我会再次提出这个问题,但不同的是,希望得到更全面和更清晰的信息。 >

在我的情况下,我想用代表人物的对象进行小型模拟。几个 Person()类的实例将被创建。它应该存在一段时间,直到它实际上死亡,而其他实例将被创建。



现在我如何使 Person() instancedie(假设这些实例中有很多会被创建,并且我不希望这些实例像鬼魂一样挂出)?

有几种方法可以引用一个对象:

  john = Person('john')




$ b $ p $ people = []
people.append(Person('john'))

  people = {} 
people ['john'] = Person('john')

保持我的程序清洁,最佳地释放资源的最佳方法是什么?那么引用我的对象的最好方法是什么,这样我就可以控制对象的删除了?

程序很自然地创建和处理对象,所以我从不担心它。



一些例子:

  person = Person('john')
person = Person('james')
#哎呀! '约翰'已经死了!

people = []
people.append(Person('john'))
#...
#所有'人'住在人们
人= []
#现在所有的'人'都死了(包括引用他们的列表)
$ b $ class House():
def setOwner(self,person):
self.owner = person

house.setOwner(people [0])
#现在House指的是一个人
people = []
#现在所有'人'都死了,除了house.owner所指的那个。

我假设您接下来是这样的:

  people = {} 
people ['john'] = Person('john')

def removePerson(personName):
del person [personName]

removePerson('john')

这个例子 people 是主列表,您可以控制何时 Person 被添加并从列表中移除(其a字典)。



您可能需要仔细考虑创建人员的概念,然后非常彻底地死去:一旦创建了人员首次与模拟进行交互的方式。死后,你应该如何解开参考文献? (对于一个人来说,可以引用其他的东西,例如 House 在我的示例中可以保持一个人的活力。您可以让其他对象保留名称的人)。

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.

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.

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')

or

people = []
people.append(Person('john'))

or

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.

Some examples:

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.

What I assume you are after is this:

people = {}
people['john'] = Person('john')

def removePerson(personName):
    del people[personName]

removePerson('john')

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).

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垃圾回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆