跟踪子类和实例? [英] Keeping track of subclasses and instances?

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

问题描述




跟踪用户自定义子类的最佳方式是什么?

这些子类的实例是什么?我只需要一个正确方向的指针......谢谢。


-

Karlo Lozovina - Mosor

解决方案

Karlo Lozovina写道:





什么是跟踪用户自定义子类的最佳方法,以及这些子类的实例是什么?我只需要一个指向正确方向的指针......谢谢。



我不完全确定我理解这个问题但是现在这样。

类的实例可以存储在列表或词典中。在列表中你

通过索引引用它们(或迭代它们),在词典中你可以给它们一个用作键的名字。


希望这会有所帮助。


-Larry


Larry Bates写道:
< blockquote class =post_quotes>
我不完全确定我理解这个问题但是这里有。

实例

类可以存储类在列表或词典中。在列表中,您需要通过索引(或迭代它们)和字典来引用它们。

您可以为它们指定一个用作密钥的名称。



我希望如果那么简单:)。


这是一个更长的描述 - 我有一个函数给出输入创建一个

自定义类并将其返回。用户可以自由地进行子类化(甚至更多,他应该这样做),当然他会创建那些

子类的实例。现在,我的问题是如何跟踪子类及其

实例,而无需用户交互(将它们附加到列表,

或添加到字典中)?


谢谢,


-

Karlo Lozovina - Mosor


10月10日,9:19 pm,Karlo Lozovina< _kar ... @ mosor.netwrote:


Larry Bates写道:


我不完全确定我理解这个问题但是这里有。

实例

类是类可以存储在列表或词典中。在列表中,您需要通过索引(或迭代它们)和字典来引用它们。

您可以为它们指定一个用作密钥的名称。



我希望如果那么简单:)。


这是一个更长的描述 - 我有一个函数给出输入创建一个

自定义类并将其返回。用户可以自由地进行子类化(甚至更多,他应该这样做),当然他会创建那些

子类的实例。现在,我的问题是如何跟踪子类及其

实例,而无需用户交互(将它们附加到列表,

或添加到字典中)?


谢谢,


-

Karlo Lozovina - Mosor



我猜Larry的实际问题是为什么*你需要跟踪

用户的实例和子类,不要让他们跟踪他们的

如果/何时需要它们。我知道

这是合法的用例,但这并不典型。无论如何,这里有一些东西可以让你

开始;所有用户必须做的是(直接或间接)从

InstanceTracker派生,如果C类定义__init__,

super(C,self).__ init __()应该明确调用:

来自集合import deque

来自weakref import WeakKeyDictionary


class InstanceTracker(object):

def __init __(自我):

尝试:all = self .__ class __.__ dict __ [''__ instances__'']

除了KeyError:

self .__ class __.__ instances__ = all = WeakKeyDictionary()

all [self] =无


def iter_instances(cls):

返回iter(cls .__ dict __。get(''__ instances __'',[]))


def iter_descendant_classes(cls):

memo = set()

unvisited = deque(cls .__ subclasses __())

unvisited:

top = unvisited.popleft()<如果top不在备忘录中,则为


memo.add(top); yield top

unvisited.extend(top .__ subclasses __())


#----- example ----------- ---------------------------

if __name__ ==''__ main__'':


A级(InstanceTracker):通过

B1级(A):通过

B2级(A):通过

class C1(B1,B2):

def __init __(self):

super(C1,self).__ init __()

C2级(B1,B2):通过

D级(C1,C2):通过


项目= [A(),B1(),B2 (),C1(),C1(),D(),A(),B2()]

print''*每班实例''

for c in iter_descendant_classes(A):

print c,list(iter_instances(c))


print''*每个类的实例(删除后)''

del items

for c in iter_descendant_classes(A):

print c,list(iter_instances(c))

HTH,

乔治


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

解决方案

Karlo Lozovina wrote:

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.

I''m not completely sure I understand the question but here goes. Instances of
classes are classes can be stored in lists or dictionaries. In lists you
reference them via their index (or iterate over them) and in dictionaries you
can give them a name that is used as a key.

Hope this helps.

-Larry


Larry Bates wrote:

I''m not completely sure I understand the question but here goes.
Instances of
classes are classes can be stored in lists or dictionaries. In lists you
reference them via their index (or iterate over them) and in dictionaries
you can give them a name that is used as a key.

I wish if it were that simple :).

Here is a longer description - I have a function that given input creates a
custom class and returns it back. The user is free to subclass that (even
more, he should do that), and of course he will make instances of those
subclasses. Now, my question is how to keep track of subclasses and their
instances, without the need for user interaction (appending them to a list,
or adding to dictionary)?

Thanks,

--
Karlo Lozovina - Mosor


On Oct 10, 9:19 pm, Karlo Lozovina <_kar...@mosor.netwrote:

Larry Bates wrote:

I''m not completely sure I understand the question but here goes.
Instances of
classes are classes can be stored in lists or dictionaries. In lists you
reference them via their index (or iterate over them) and in dictionaries
you can give them a name that is used as a key.


I wish if it were that simple :).

Here is a longer description - I have a function that given input creates a
custom class and returns it back. The user is free to subclass that (even
more, he should do that), and of course he will make instances of those
subclasses. Now, my question is how to keep track of subclasses and their
instances, without the need for user interaction (appending them to a list,
or adding to dictionary)?

Thanks,

--
Karlo Lozovina - Mosor


I guess Larry''s actual question was why do *you* need to keep track of
users'' instances and subclasses and don''t let them keep track on their
own if/when they need it. I realize there are legitimate use cases for
this but it''s not typical. Anyway, here''s something to get you
started; all a user has to do is derive (directly or indirectly) from
InstanceTracker and, if a class C defines __init__,
super(C,self).__init__() should be called explicitly:
from collections import deque
from weakref import WeakKeyDictionary

class InstanceTracker(object):
def __init__(self):
try: all = self.__class__.__dict__[''__instances__'']
except KeyError:
self.__class__.__instances__ = all = WeakKeyDictionary()
all[self] = None

def iter_instances(cls):
return iter(cls.__dict__.get(''__instances__'',[]))

def iter_descendant_classes(cls):
memo = set()
unvisited = deque(cls.__subclasses__())
while unvisited:
top = unvisited.popleft()
if top not in memo:
memo.add(top); yield top
unvisited.extend(top.__subclasses__())

#----- example --------------------------------------
if __name__ == ''__main__'':

class A(InstanceTracker): pass
class B1(A): pass
class B2(A): pass
class C1(B1,B2):
def __init__(self):
super(C1,self).__init__()
class C2(B1,B2): pass
class D(C1,C2): pass

items = [A(),B1(),B2(),C1(),C1(),D(),A(),B2()]
print '' * Instances per class''
for c in iter_descendant_classes(A):
print c, list(iter_instances(c))

print '' * Instances per class (after delete)''
del items
for c in iter_descendant_classes(A):
print c, list(iter_instances(c))
HTH,
George


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

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