获取一个类的所有实例 [英] Get all the instances of one class

查看:79
本文介绍了获取一个类的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否有一种简单的方法来获取一个类的所有实例?我的意思是

没有任何额外的改变。


br,Terry

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

推荐答案

Terry< te ********** @ gmail.comwrites:
Terry <te**********@gmail.comwrites:

是否有一种简单的方法来获取所有实例一节课?
Is there a simple way to get all the instances of one class?



定义全部。当前范围内的所有实例?


也许更相关:你想解决什么问题?


-

\ a ??我们要求严格定义的疑点和不确定区域! |

` \ a ?? Vroomfondel,_The Hitch-Hiker'的银河指南_,|

_o__)Douglas Adams |

Ben Finney

Define "all". All the instances in the current scope?

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

--
\ a??We demand rigidly defined areas of doubt and uncertainty!a?? |
`\ a??Vroomfondel, _The Hitch-Hiker''s Guide To The Galaxy_, |
_o__) Douglas Adams |
Ben Finney


En Fri,2008年5月16日20:44:00 -0300,Terry< te ********** @ gmail.com> ;
$ b $bescribió:
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.



尝试使用gc.get_referrers()

pyimport gc

pyclass A(对象) :传递

....

pya,b,c = A(),A(),A()

pyA

< class __main __。在0x00A3F4E0>

gc.get_referrers(a)中的项目:打印类型(项目)

....

< type''getset_descriptor''>

< type''getset_descriptor''>

< type''tuple ''>

< class''__ main __。A''>

< class''__ main __。A''>

< class''__ main __。A''>

< type''dict''>

< type''dict''>


我们需要过滤该列表,只保留A'的实例:


py [gc.get_referrers中项目的项目(A )if isinstance(item,A)]

[< __ main __。0x00A40DC8处的对象>,< __ main __。0x00A40DF0处的对象>,

< __ main__。 0x00A40E18处的对象>]

-

Gabriel Genellina

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


5月17日上午8:04 ,Gabriel Genellina, < gagsl -... @ yahoo.com.ar>

写道:
On May 17, 8:04 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

En Fri,2008年5月16日20:44: 00 -0300,Terry< terry.yin ... @ gmail.com>
$ b $bescribió:
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.



尝试使用gc.get_referrers()

pyimport gc

pyclass A(对象) :通过

...

pya,b,c = A(),A(),A()

pyA
< class __main __。在0x00A3F4E0>

pyc项目在gc.get_referrers(A)中:打印类型(项目)

...

< type''getset_descriptor''>

< type''getset_descriptor''>

< type''tuple'' >

< class''__ main __。A''>

< class''_ _ main __。A''>

< class''__ main __。A''>

< type''dict''>

< type''dict''>


我们需要过滤该列表,只保留A'的实例:


py [gc.get_referrers(A)中项目的项目如果isinstance(item,A)]

[< __ main __。0x00A40DC8处的对象>,< __ main __。0x00A40DF0处的对象>,

< __ main __。一个对象在0x00A40E18>]


-

Gabriel Genellina


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.


这篇关于获取一个类的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆