Class and instance related questions.

Chris Angelico rosuav at gmail.com
Fri Jan 24 15:45:30 EST 2014


On Sat, Jan 25, 2014 at 7:32 AM, Asaf Las <roegltd at 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... at 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.

> And usage of weak reference was suggested only to allow class instances
> garbage collected if for example class static container attribute will be
> used as class instance reference storage?

Weak references mean that the objects will be disposed of as normal,
but that you'll know that they've gone.

>> > Another question - if class is object is it possible
>> > to delete it? If it is possible then how instances
>> > of that class will behave?
>>
>> It's possible to unbind the name, but every instance retains a
>> reference to its class, so the class itself won't disappear until
>> there are no instances left of it.
>>
>> ChrisA
>
> That is interesting. Is it also reference count mechanism or something else?

Yes. [1]

ChrisA

[1] Within the bounds of the question asked, I think a simple "Yes" is
more useful here than a long and detailed explanation of the many
Pythons and how not all of them refcount at all. Consider this
footnote my apology to the experts who would otherwise feel that I'm
majorly misleading the OP. Sorry.



More information about the Python-list mailing list