Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64713
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Class and instance related questions. |
| Date | 2014-01-24 20:08 -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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5961.1390612139.18130.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Find similar | Unroll 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