Guest User

Untitled

a guest
May 18th, 2018
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. class Test():
  4.  
  5.     instances = []
  6.  
  7.     def __init__(self, name):
  8.         self.name = name
  9.         self.register(self)
  10.         #vagy legyen inkább: self.__class__.register(self) ?
  11.  
  12.  
  13.     @classmethod
  14.     def register(cls,instance):
  15.         cls.instances.append(instance)
  16.  
  17.     @classmethod
  18.     def kill(cls,name):
  19.         for instance in cls.instances:
  20.             if instance.name == name:
  21.                 cls.instances.remove(instance)
  22.  
  23.     @classmethod
  24.     def print_instances(cls):
  25.         for instance in cls.instances:
  26.             print(instance, instance.name)
  27.            
  28. for name in ['Alpha', 'Beta', 'Gamma']:
  29.     Test(name)
  30.  
  31. Test.print_instances()
  32.  
  33. Test.kill('Alpha')
  34.  
  35. Test.print_instances()
Advertisement
Add Comment
Please, Sign In to add comment