473,499 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get all the instances of one class

Hi,

Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.

br, Terry
Jun 27 '08 #1
10 5503
Terry <te**********@gmail.comwrites:
Is there a simple way to get all the instances of one class?
Define "all". All the instances in the current scope?

Perhaps more pertinent: What problem are you trying to address?

--
\ “We demand rigidly defined areas of doubt and uncertainty!” |
`\ —Vroomfondel, _The Hitch-Hiker's Guide To The Galaxy_, |
_o__) Douglas Adams |
Ben Finney
Jun 27 '08 #2
En Fri, 16 May 2008 20:44:00 -0300, Terry <te**********@gmail.com>
escribi:
Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.
Try with gc.get_referrers()

pyimport gc
pyclass A(object): pass
....
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrers(A): print type(item)
....
<type 'getset_descriptor'>
<type 'getset_descriptor'>
<type 'tuple'>
<class '__main__.A'>
<class '__main__.A'>
<class '__main__.A'>
<type 'dict'>
<type 'dict'>

We need to filter that list, keeping only A's instances:

py[item for item in gc.get_referrers(A) if isinstance(item,A)]
[<__main__.A object at 0x00A40DC8>, <__main__.A object at 0x00A40DF0>,
<__main__.A object at 0x00A40E18>]

--
Gabriel Genellina

Jun 27 '08 #3
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@gmail.com>
escribi:
Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.

Try with gc.get_referrers()

pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrers(A): print type(item)
...
<type 'getset_descriptor'>
<type 'getset_descriptor'>
<type 'tuple'>
<class '__main__.A'>
<class '__main__.A'>
<class '__main__.A'>
<type 'dict'>
<type 'dict'>

We need to filter that list, keeping only A's instances:

py[item for item in gc.get_referrers(A) if isinstance(item,A)]
[<__main__.A object at 0x00A40DC8>, <__main__.A object at 0x00A40DF0>,
<__main__.A object at 0x00A40E18>]

--
Gabriel Genellina
Thanks! This is what I'm looking for.
Jun 27 '08 #4
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@gmail.com>
escribi:
Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.

Try with gc.get_referrers()

pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrers(A): print type(item)
...
<type 'getset_descriptor'>
<type 'getset_descriptor'>
<type 'tuple'>
<class '__main__.A'>
<class '__main__.A'>
<class '__main__.A'>
<type 'dict'>
<type 'dict'>

We need to filter that list, keeping only A's instances:

py[item for item in gc.get_referrers(A) if isinstance(item,A)]
[<__main__.A object at 0x00A40DC8>, <__main__.A object at 0x00A40DF0>,
<__main__.A object at 0x00A40E18>]

--
Gabriel Genellina
But I saw in the help that we should "Avoid using get_referrers() for
any purpose other than debugging. "
Jun 27 '08 #5
Terry schrieb:
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@gmail.com>
escribi:
>>Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.
Try with gc.get_referrers()

pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrers(A): print type(item)
...
<type 'getset_descriptor'>
<type 'getset_descriptor'>
<type 'tuple'>
<class '__main__.A'>
<class '__main__.A'>
<class '__main__.A'>
<type 'dict'>
<type 'dict'>

We need to filter that list, keeping only A's instances:

py[item for item in gc.get_referrers(A) if isinstance(item,A)]
[<__main__.A object at 0x00A40DC8>, <__main__.A object at 0x00A40DF0>,
<__main__.A object at 0x00A40E18>]

--
Gabriel Genellina

But I saw in the help that we should "Avoid using get_referrers() for
any purpose other than debugging. "
Yes, because using it do is very resource-consuming and shouldn't be
done. Why don't you tell us what you are after here & then we might come
up with a better solution?

Diez
Jun 27 '08 #6
En Sun, 18 May 2008 12:32:28 -0300, Terry <te**********@gmail.comescribi:
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@gmail.com>
escribi:
Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.

Try with gc.get_referrers()

But I saw in the help that we should "Avoid using get_referrers() for
any purpose other than debugging. "
Yes - so what do you want to do actually?

--
Gabriel Genellina

Jun 27 '08 #7

On 17 maj 2008, at 01.44, Terry wrote:
Hi,

Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.

br, Terry
--
http://mail.python.org/mailman/listinfo/python-list

class MyClass : a_base_class
memberlist=[]

# Insert object in memberlist when created;
# note: objects won't be garbage collected until removed from
memberlist.
-------------------------------------
This sig is dedicated to the advancement of Nuclear Power
Tommy Nordgren
to************@comhem.se


Jun 27 '08 #8
On May 18, 11:35 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Terry schrieb:
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@gmail.com>
escribi:
>Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.
Try with gc.get_referrers()
pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrers(A): print type(item)
...
<type 'getset_descriptor'>
<type 'getset_descriptor'>
<type 'tuple'>
<class '__main__.A'>
<class '__main__.A'>
<class '__main__.A'>
<type 'dict'>
<type 'dict'>
We need to filter that list, keeping only A's instances:
py[item for item in gc.get_referrers(A) if isinstance(item,A)]
[<__main__.A object at 0x00A40DC8>, <__main__.A object at 0x00A40DF0>,
<__main__.A object at 0x00A40E18>]
--
Gabriel Genellina
But I saw in the help that we should "Avoid using get_referrers() for
any purpose other than debugging. "

Yes, because using it do is very resource-consuming and shouldn't be
done. Why don't you tell us what you are after here & then we might come
up with a better solution?

Diez
I'm developing a message/state_machine based python program (I'm not
using stackless, plan to move to it later).
I want to collect all the state_machines (threads) and send them
'tick' message or 'quit' message.

Now I'm using a static member to collect the machines, just wonderring
if python already provide something like this.
Jun 27 '08 #9
Tommy Nordgren wrote:
class MyClass : a_base_class
memberlist=[]

# Insert object in memberlist when created;
# note: objects won't be garbage collected until removed from memberlist.
Just to say, if you wanted to go about it that way, you could avoid the
garbage collection problem by using weakrefs:

<http://docs.python.org/lib/module-weakref.html>
--
Jun 27 '08 #10
Matt Nordhoff <mn*******@mattnordhoff.comwrote:
Tommy Nordgren wrote:
class MyClass : a_base_class
memberlist=[]

# Insert object in memberlist when created;
# note: objects won't be garbage collected until removed from memberlist.

Just to say, if you wanted to go about it that way, you could avoid the
garbage collection problem by using weakrefs:

<http://docs.python.org/lib/module-weakref.html>
Eg...

from weakref import WeakKeyDictionary

class Test(object):
_instances = WeakKeyDictionary()
def __init__(self):
self._instances[self] = True
# your normal init stuff here
@classmethod
def instances(cls):
return cls._instances.keys()

print Test.instances()
a = Test()
b = Test()
print Test.instances()
del a
print Test.instances()
del b
print Test.instances()

.....

Which prints

[]
[<__main__.Test object at 0xb7d4eb6c>, <__main__.Test object at 0xb7d4eb4c>]
[<__main__.Test object at 0xb7d4eb6c>]
[]
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2183
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
4
1306
by: Brad Tilley | last post by:
I've just started using classes in Python for some projects at work and have a few questions about them. I understand that once a class is defined that I can create many instances of it like this:...
8
1815
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
90
4291
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
3
1647
by: Quenton Bonds | last post by:
Hello I am trying to understand the abilities and limitation of creating an instance. First I will give you my understanding then please steer me in the right direction. Abiities 1. The two...
4
3284
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
4
3127
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
0
1217
by: bettatronic | last post by:
The goal : to have class which is able to add some OLE objects on the sheet runtime. Instances of the class must be accessible from any public/private module. Okay, I've experimented for so long &...
10
2255
by: Karlo Lozovina | last post by:
Hi, what's the best way to keep track of user-made subclasses, and instances of those subclasses? I just need a pointer in a right direction... thanks. -- Karlo Lozovina -- Mosor
0
1040
by: Ignace 'a0a' Saenen | last post by:
Hi, I have actually found 2 ways to do this. One uses the XmlInclude attribute in the base class A, specifying the valid derived types to deserialize from the stream, and one uses one of the 8...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.