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


Groups > comp.lang.python > #64693 > unrolled thread

Class and instance related questions.

Started byAsaf Las <roegltd@gmail.com>
First post2014-01-24 08:31 -0800
Last post2014-01-24 20:08 -0500
Articles 9 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#64693 — Class and instance related questions.

FromAsaf Las <roegltd@gmail.com>
Date2014-01-24 08:31 -0800
SubjectClass and instance related questions.
Message-ID<b781b5d3-5dc0-4a7e-9792-78aadf2d6ab4@googlegroups.com>
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

[toc] | [next] | [standalone]


#64695

FromChris Angelico <rosuav@gmail.com>
Date2014-01-25 03:37 +1100
Message-ID<mailman.5949.1390581460.18130.python-list@python.org>
In reply to#64693
On Sat, Jan 25, 2014 at 3:31 AM, Asaf Las <roegltd@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.

> 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

[toc] | [prev] | [next] | [standalone]


#64699

FromAsaf Las <roegltd@gmail.com>
Date2014-01-24 12:32 -0800
Message-ID<2c8035fe-7514-4598-9497-c2f90f9291c2@googlegroups.com>
In reply to#64695
Hi Chris 

Thanks for answers 

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? 

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?

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

Thanks 

Asaf

[toc] | [prev] | [next] | [standalone]


#64700

FromChris Angelico <rosuav@gmail.com>
Date2014-01-25 07:45 +1100
Message-ID<mailman.5952.1390596339.18130.python-list@python.org>
In reply to#64699
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.

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

[toc] | [prev] | [next] | [standalone]


#64701

FromAsaf Las <roegltd@gmail.com>
Date2014-01-24 13:03 -0800
Message-ID<7f93cb3d-576c-4929-a815-2a5ea4fcf732@googlegroups.com>
In reply to#64700
On Friday, January 24, 2014 10:45:30 PM UTC+2, Chris Angelico wrote:
> On Sat, Jan 25, 2014 at 7:32 AM, Asaf Las <r....@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.
> 
> > 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.

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

Thanks

Asaf

[toc] | [prev] | [next] | [standalone]


#64703

FromChris Angelico <rosuav@gmail.com>
Date2014-01-25 08:18 +1100
Message-ID<mailman.5953.1390598298.18130.python-list@python.org>
In reply to#64701
On Sat, Jan 25, 2014 at 8:03 AM, Asaf Las <roegltd@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#64704

FromAsaf Las <roegltd@gmail.com>
Date2014-01-24 14:08 -0800
Message-ID<c47d4d9c-69f7-4d6a-aaad-7a45ae3ae6b0@googlegroups.com>
In reply to#64703
On Friday, January 24, 2014 11:18:08 PM UTC+2, Chris Angelico wrote:
> On Sat, Jan 25, 2014 at 8:03 AM, Asaf Las <r....@gmail.com> wrote:
> > 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

I appreciate your kind words!

Thanks 

Asaf

[toc] | [prev] | [next] | [standalone]


#64706

FromDave Angel <davea@davea.name>
Date2014-01-24 18:58 -0500
Message-ID<mailman.5955.1390607789.18130.python-list@python.org>
In reply to#64701
 Asaf Las <roegltd@gmail.com> Wrote in message:
> On Friday, January 24, 2014 10:45:30 PM UTC+2, Chris Angelico wrote:
>> On Sat, Jan 25, 2014 at 7:32 AM, Asaf Las <r....@gmail.com> wrote:
>> > On Friday, January 24, 2014 6:37:29 PM UTC+2, Chris Angelico wrote:
>> 
>> >> 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.
 


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#64713

FromTerry Reedy <tjreedy@udel.edu>
Date2014-01-24 20:08 -0500
Message-ID<mailman.5961.1390612139.18130.python-list@python.org>
In reply to#64699
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web