Class and instance related questions.

A

Asaf Las

Hi

Is there way to get list of instances of particular
class through class itself? via metaclass or any other method?

Another question - if class is object is it possible
to delete it? If it is possible then how instances
of that class will behave?

Thanks

Asaf
 
C

Chris Angelico

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.
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
 
A

Asaf Las

Hi Chris

Thanks for answers

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?

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?
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?

Thanks

Asaf
 
C

Chris Angelico

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.
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.
 
A

Asaf Las

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.
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.

Chris, i like answers which open doors to my curiosity :)
yet i should spend my credits very carefully :)

Thanks

Asaf
 
C

Chris Angelico

Chris, i like answers which open doors to my curiosity :)
yet i should spend my credits very carefully :)

Trust me, there is no limit to what you can learn when you have that
kind of curiosity! Ask more questions and you'll get more details.
Around here, we have all sorts of experts (several core Python
developers hang out here, at least one of whom posts fairly
frequently), and a good number of us have a decade or two of
experience in programming, having used a large number of languages,
and we've all settled on Python as being in some way important to us.
It's always interesting to get a discussion going with people whose
non-Python expertise differs - a couple of us (self included) here are
very familiar with REXX, some know Ruby (self NOT included), or lisp,
or go, or anything else under the sun. And then there are those of us
who'll quote Alice in Wonderland, or the Thomas the Tank Engine books,
or abstract philosophy, or Gilbert and Sullivan, or Discworld (I think
I've seen that one quoted? I'm not personally familiar, so I can't say
for sure), or Firefly, or Real Genius, or ... or ...., you get the
idea :) So you get to learn about all sorts of other nerdy interests
for free!

ChrisA
 
A

Asaf Las

Trust me, there is no limit to what you can learn when you have that
kind of curiosity! Ask more questions and you'll get more details.
Around here, we have all sorts of experts (several core Python
developers hang out here, at least one of whom posts fairly
frequently), and a good number of us have a decade or two of
experience in programming, having used a large number of languages,
and we've all settled on Python as being in some way important to us.
It's always interesting to get a discussion going with people whose
non-Python expertise differs - a couple of us (self included) here are
very familiar with REXX, some know Ruby (self NOT included), or lisp,
or go, or anything else under the sun. And then there are those of us
who'll quote Alice in Wonderland, or the Thomas the Tank Engine books,
or abstract philosophy, or Gilbert and Sullivan, or Discworld (I think
I've seen that one quoted? I'm not personally familiar, so I can't say
for sure), or Firefly, or Real Genius, or ... or ...., you get the
idea :) So you get to learn about all sorts of other nerdy interests
for free!
ChrisA

I appreciate your kind words!

Thanks

Asaf
 
D

Dave Angel

Asaf Las said:
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.

Chris, i like answers which open doors to my curiosity :)
yet i should spend my credits very carefully :)
Rather than dwelling on reference counting, which is just one
mechanism for identifying and disposing of objects, it's usually
more fruitful to consider what it means to be unreferenced.


The usual term is "reachable. " If an object cannot be reached by
following some chain of references, starting from a small list
of kernel items, then it should be freed. The cheap way of
noticing some such objects is by noticing the reference count go
to zero. That's not enough, however, so you also need to
periodically run a sweep type of garbage collector. See if you
can guess why reference count alone is inadequate.


The CPython system uses both of these, but other implementations
do not. I believe that I've heard that the jython system does
nothing at all, just letting the Java runtime handle it.
 
T

Terry Reedy


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top