<tfoot id='Lnkd7'></tfoot>
    • <bdo id='Lnkd7'></bdo><ul id='Lnkd7'></ul>

        <small id='Lnkd7'></small><noframes id='Lnkd7'>

        <legend id='Lnkd7'><style id='Lnkd7'><dir id='Lnkd7'><q id='Lnkd7'></q></dir></style></legend>
        <i id='Lnkd7'><tr id='Lnkd7'><dt id='Lnkd7'><q id='Lnkd7'><span id='Lnkd7'><b id='Lnkd7'><form id='Lnkd7'><ins id='Lnkd7'></ins><ul id='Lnkd7'></ul><sub id='Lnkd7'></sub></form><legend id='Lnkd7'></legend><bdo id='Lnkd7'><pre id='Lnkd7'><center id='Lnkd7'></center></pre></bdo></b><th id='Lnkd7'></th></span></q></dt></tr></i><div id='Lnkd7'><tfoot id='Lnkd7'></tfoot><dl id='Lnkd7'><fieldset id='Lnkd7'></fieldset></dl></div>

        如何将对象交给python垃圾收集?

        时间:2023-07-05

              <tbody id='036wH'></tbody>
              <bdo id='036wH'></bdo><ul id='036wH'></ul>

              <small id='036wH'></small><noframes id='036wH'>

              <i id='036wH'><tr id='036wH'><dt id='036wH'><q id='036wH'><span id='036wH'><b id='036wH'><form id='036wH'><ins id='036wH'></ins><ul id='036wH'></ul><sub id='036wH'></sub></form><legend id='036wH'></legend><bdo id='036wH'><pre id='036wH'><center id='036wH'></center></pre></bdo></b><th id='036wH'></th></span></q></dt></tr></i><div id='036wH'><tfoot id='036wH'></tfoot><dl id='036wH'><fieldset id='036wH'></fieldset></dl></div>
              <tfoot id='036wH'></tfoot>
            • <legend id='036wH'><style id='036wH'><dir id='036wH'><q id='036wH'></q></dir></style></legend>
                • 本文介绍了如何将对象交给python垃圾收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  上一篇:python中的引用循环是什么? 下一篇:来自 sys.getrefcount 的意外结果

                  相关文章

                    <small id='yAYNj'></small><noframes id='yAYNj'>

                  1. <i id='yAYNj'><tr id='yAYNj'><dt id='yAYNj'><q id='yAYNj'><span id='yAYNj'><b id='yAYNj'><form id='yAYNj'><ins id='yAYNj'></ins><ul id='yAYNj'></ul><sub id='yAYNj'></sub></form><legend id='yAYNj'></legend><bdo id='yAYNj'><pre id='yAYNj'><center id='yAYNj'></center></pre></bdo></b><th id='yAYNj'></th></span></q></dt></tr></i><div id='yAYNj'><tfoot id='yAYNj'></tfoot><dl id='yAYNj'><fieldset id='yAYNj'></fieldset></dl></div>
                      <bdo id='yAYNj'></bdo><ul id='yAYNj'></ul>

                  2. <tfoot id='yAYNj'></tfoot>
                    1. <legend id='yAYNj'><style id='yAYNj'><dir id='yAYNj'><q id='yAYNj'></q></dir></style></legend>