473,624 Members | 2,534 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 5523
Terry <te**********@g mail.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**********@g mail.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_referrer s()

pyimport gc
pyclass A(object): pass
....
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrer s(A): print type(item)
....
<type 'getset_descrip tor'>
<type 'getset_descrip tor'>
<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_referrer s(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.a r>
wrote:
En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@g mail.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_referrer s()

pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrer s(A): print type(item)
...
<type 'getset_descrip tor'>
<type 'getset_descrip tor'>
<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_referrer s(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.a r>
wrote:
En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@g mail.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_referrer s()

pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrer s(A): print type(item)
...
<type 'getset_descrip tor'>
<type 'getset_descrip tor'>
<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_referrer s(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.a r>
wrote:
>En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@g mail.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_referrer s()

pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrer s(A): print type(item)
...
<type 'getset_descrip tor'>
<type 'getset_descrip tor'>
<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_referrer s(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**********@g mail.comescribi :
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@g mail.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_referrer s()

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.w eb.dewrote:
Terry schrieb:
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Fri, 16 May 2008 20:44:00 -0300, Terry <terry.yin...@g mail.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_referrer s()
pyimport gc
pyclass A(object): pass
...
pya,b,c = A(),A(),A()
pyA
<class __main__.A at 0x00A3F4E0>
pyfor item in gc.get_referrer s(A): print type(item)
...
<type 'getset_descrip tor'>
<type 'getset_descrip tor'>
<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_referrer s(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

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

Similar topics

7
2192
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 x in range(10):
4
1321
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: class xyz: def one(): pass def two (): pass def three():
8
1824
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' handles) instances? Thanks in advance for any help
90
4370
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 immutable instances? -- \ "Love is the triumph of imagination over intelligence." -- |
3
1657
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 ways to create an instance is def method(self) & __int__(self, other, instances,...) 2. By creating an instance of a method; the functions of that method can be used through out the
4
3305
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 custom metaclass with a __getattribute__ method, the method is used when acessing some attribute directly with the class object, but not when you do it from the instance.
4
3139
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
1225
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 & getting quite tired (:huh: ) of it. So I think it's time to call some help. I hope there must be some solution. I NEED it. Well actually what I need ? I need to have some class/object which create some OleObjects (images and buttons) runtime....
10
2269
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
1068
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 XmlSerializer ctor's to specify base class A and derived class B relationships. However, XmlInclude only works at compile-time, and I would like to support serialisation for future derived classes C in other modules. So in this case, this option...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8330
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4075
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
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 we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.