Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'classes,': 0.05; 'cpython': 0.05; 'attribute': 0.07; 'correct.': 0.07; 'method.': 0.07; 'returned.': 0.07; '__init__': 0.09; 'attributes': 0.09; 'classes.': 0.09; 'excluding': 0.09; 'friday,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:instance': 0.09; 'subject:related': 0.09; 'jan': 0.12; 'question.': 0.14; '24,': 0.16; 'ahead,': 0.16; 'metaclass': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'module': 0.19; "python's": 0.19; 'seems': 0.21; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'fraction': 0.24; 'references': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'code': 0.31; '25,': 0.31; '>>>>': 0.31; 'are.': 0.31; 'class': 0.32; 'classes': 0.35; 'objects': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'idle': 0.36; 'instances': 0.36; 'january': 0.37; 'las': 0.37; 'list': 0.37; 'depends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'track': 0.38; 'to:addr:python.org': 0.39; 'system.': 0.39; 'received:org': 0.40; 'numbers': 0.61; 'received:173': 0.61; 'such': 0.63; 'believe': 0.68; '"not': 0.84; 'itself?': 0.84; 'received:fios.verizon.net': 0.84; '150': 0.91; 'shell,': 0.91; 'subject:Class': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Class and instance related questions. Date: Fri, 24 Jan 2014 20:08:41 -0500 References: <2c8035fe-7514-4598-9497-c2f90f9291c2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390612139 news.xs4all.nl 2873 [2001:888:2000:d::a6]:55392 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64713 On 1/24/2014 3:45 PM, Chris Angelico wrote: > On Sat, Jan 25, 2014 at 7:32 AM, Asaf Las wrote: >> On Friday, January 24, 2014 6:37:29 PM UTC+2, Chris Angelico wrote: >>> On Sat, Jan 25, 2014 at 3:31 AM, Asaf Las wrote: >>>> Hi >>>> Is there way to get list of instances of particular >>>> class through class itself? via metaclass or any other method? >>> Not automatically, but you can make a class that keeps track of its >>> instances with a weak reference system. >> >> By "not automatically" do you mean there is no way to get references to instances of class via python's provided methods or attributes for class >> object at time the class object is created? > > Correct. This depends on exactly the meaning of your question. CPython has a gc module which has this method. gc.get_objects() Returns a list of all objects tracked by the collector, excluding the list returned. In a fresh 3.4.0 Idle shell, the list has about 15000 objects in about 150 classes. (A substantial fraction of the classes, at least, are Idle classes used in the user process.) Numbers and strings are not tracked. I believe instances of python-coded classes always are. So you can brute force search all tracked instances for instances of a class you code in Python, but this in not 'through [the] class itself'. If you plan ahead, it seems better to use a class attribute such as _instances = weakref.WeakSet() to contain them and add them in the __init__ method. -- Terry Jan Reedy