Edit detail for PocetInstanciObjektu revision 1 of 1

1
Editor: geon
Time: 2006/02/18 15:05:17 GMT+0
Note:

changed:
-
Počet instancí objektu

 Ahoj, dá se nejak zjistit, kolik je aktivních instancí daného
objektu a získat na ne reference?

 První příklad
 
  ::

    class MyClass:

       counter = 0
       def __init__(self):
           MyClass.counter += 1
       def __del__(self):
           MyClass.counter -= 1
    
    
    print MyClass.counter
    c = MyClass()
    print MyClass.counter
    d = MyClass()
    print MyClass.counter
    c = MyClass()
    print MyClass.counter


 Druhý příklad
 
  ::
 
    class MojeTrida:

        inst=[]
        def __init__(self):
            self.__class__.inst.append(self)
    
    c=MojeTrida()
    c2=MojeTrida()
    c3=MojeTrida()
  
  Nevýhoda: instance se po zániku nebudou uvolňovat.
  
 Třetí příklad - slabé reference
 
  ::
  
    #!/usr/bin/python
    import weakref, gc
    
    class Counted:

        reflist = []
        def __init__(self):
            Counted.reflist.append(weakref.ref(self))
        def get_count(self):
            self.reflist = [x for x in self.reflist if x() != None]
            return len(self.reflist)
        get_count = classmethod(get_count)
        def get_instances(self):
            self.reflist = [x for x in self.reflist if x() != None]
            t = [x() for x in self.reflist]
            return [x for x in t if x != None]
        get_instances = classmethod(get_instances)
    
    a = Counted()
    print Counted.get_count()
    b = Counted()
    print Counted.get_count()
    b = Counted()
    print Counted.get_count()
    del b
    print Counted.get_count()
    print Counted.get_instances()
    del a
    print Counted.get_count()
    print Counted.get_instances()
    
    c = Counted()
    print gc.get_referrers(c)
    a = [c, Counted(), Counted()]
    print gc.get_referents(a)
    print gc.get_referrers(c)
    
    print Counted.get_instances()

Počet instancí objektu

Ahoj, dá se nejak zjistit, kolik je aktivních instancí daného objektu a získat na ne reference?

První příklad

    class MyClass:

       counter = 0
       def __init__(self):
           MyClass.counter += 1
       def __del__(self):
           MyClass.counter -= 1

    print MyClass.counter
    c = MyClass()
    print MyClass.counter
    d = MyClass()
    print MyClass.counter
    c = MyClass()
    print MyClass.counter

Druhý příklad

    class MojeTrida:

        inst=[]
        def __init__(self):
            self.__class__.inst.append(self)

    c=MojeTrida()
    c2=MojeTrida()
    c3=MojeTrida()

Nevýhoda: instance se po zániku nebudou uvolňovat.

Třetí příklad - slabé reference

    #!/usr/bin/python
    import weakref, gc

    class Counted:

        reflist = []
        def __init__(self):
            Counted.reflist.append(weakref.ref(self))
        def get_count(self):
            self.reflist = [x for x in self.reflist if x() != None]
            return len(self.reflist)
        get_count = classmethod(get_count)
        def get_instances(self):
            self.reflist = [x for x in self.reflist if x() != None]
            t = [x() for x in self.reflist]
            return [x for x in t if x != None]
        get_instances = classmethod(get_instances)

    a = Counted()
    print Counted.get_count()
    b = Counted()
    print Counted.get_count()
    b = Counted()
    print Counted.get_count()
    del b
    print Counted.get_count()
    print Counted.get_instances()
    del a
    print Counted.get_count()
    print Counted.get_instances()

    c = Counted()
    print gc.get_referrers(c)
    a = [c, Counted(), Counted()]
    print gc.get_referents(a)
    print gc.get_referrers(c)

    print Counted.get_instances()