Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64713

Re: Class and instance related questions.

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 <python-python-list@m.gmane.org>
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 <tjreedy@udel.edu>
Subject Re: Class and instance related questions.
Date Fri, 24 Jan 2014 20:08:41 -0500
References <b781b5d3-5dc0-4a7e-9792-78aadf2d6ab4@googlegroups.com> <mailman.5949.1390581460.18130.python-list@python.org> <2c8035fe-7514-4598-9497-c2f90f9291c2@googlegroups.com> <CAPTjJmpS+i_7z297s-=HJ+iMY0ZYkGBi=eCW2eG+nmEX=a5aQw@mail.gmail.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 <CAPTjJmpS+i_7z297s-=HJ+iMY0ZYkGBi=eCW2eG+nmEX=a5aQw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5961.1390612139.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 1/24/2014 3:45 PM, Chris Angelico wrote:
> On Sat, Jan 25, 2014 at 7:32 AM, Asaf Las <roegltd@gmail.com> 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 <r...@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Class and instance related questions. Asaf Las <roegltd@gmail.com> - 2014-01-24 08:31 -0800
  Re: Class and instance related questions. Chris Angelico <rosuav@gmail.com> - 2014-01-25 03:37 +1100
    Re: Class and instance related questions. Asaf Las <roegltd@gmail.com> - 2014-01-24 12:32 -0800
      Re: Class and instance related questions. Chris Angelico <rosuav@gmail.com> - 2014-01-25 07:45 +1100
        Re: Class and instance related questions. Asaf Las <roegltd@gmail.com> - 2014-01-24 13:03 -0800
          Re: Class and instance related questions. Chris Angelico <rosuav@gmail.com> - 2014-01-25 08:18 +1100
            Re: Class and instance related questions. Asaf Las <roegltd@gmail.com> - 2014-01-24 14:08 -0800
          Re: Class and instance related questions. Dave Angel <davea@davea.name> - 2014-01-24 18:58 -0500
      Re: Class and instance related questions. Terry Reedy <tjreedy@udel.edu> - 2014-01-24 20:08 -0500

csiph-web