title: Differentiating between instances of a class in python
body: <p>I'm working with multiple instances of a class called Enemy. I'm calling them from my main method in the Game class, so it looks like this. </p>
  
  <pre><code>class Game(object):
       def main(self, screen):
          if condition is true:
              Enemy(arguments)
          elif condition is true:
              Enemy(arguments)
  
  class Enemy(pygame.sprite.Sprite):
      def __init__(self, location, kind, number, *groups):
          self.kind = kind
          self.number = str(number)
          # Stuff that determines where and which kind
  </code></pre>
  
  <p>each time Enemy is called it spawns an enemy.</p>
  
  <p>later on I have hit detection that needs to know which enemy is hit so it updates a dictionary. But to update that dictionary for Enemy (and other classes like it) I'll need to access each instance of Enemy and know which one I'm accessing because each have different variables about their location and such at any given time and they are all updating separately and correctly as far as I can tell. Any more information you need I'll be happy to give.</p>
  
  <p>Edit: Looking at it now I do think I simplified too much so I'm going to dump a piece of the dictionary updating code so you can look at it. Note I am a relative novice in coding and more so to classes so this may not all be very well done.</p>
  
  <pre><code>class Game(object):
      def main(self, screen):
          while 1:    
  
              self.hashMap[(topLeftx / acc, topLefty / acc)] = ["Enemy " + Enemy.number]
  
              topLeftx = Enemy.rect[0]
              topLefty = Enemy.rect[1]
              width = Enemy.rect[2]
              height = Enemy.rect[3]
              acc = 10
  
              if width &gt; acc:
                  x = math.floor(width / acc)
              for a in range(1, int(x) + 1):
                  if ((topLeftx / acc) + a, (topLefty / acc)) in self.hashMap.keys():
                      self.hashMap[((topLeftx / acc) + a, (topLefty / acc))].append("Enemy" + Enemy.number)
                  else:
                      self.hashMap[((topLeftx / acc) + a, (topLefty / acc))] = ["Enemy" + Enemy.number]
  
              if height &gt; acc:
                  y = math.floor(height / acc)
                  for b in range(1, int(y) + 1):
                      if ((topLeftx / acc), (topLefty / acc) + b) in self.hashMap.keys():
                          self.hashMap[((topLeftx / acc), (topLefty / acc) + b)].append("Enemy" + Enemy.number)
              else:
                  self.hashMap[((topLeftx / acc), (topLefty / acc) + b)] = ["Enemy" + Enemy.number]
  </code></pre>
  
  <p>This is what I tried to do for Enemy but it failed with</p>
  
  <pre><code> AttributeError: type object 'Enemy' has no attribute 'number'
  </code></pre>
  
  <p>even though it is defined in enemy. The 'number' is my take at doing ID's but it didn't work out. Any tweaks that'd make this work or something entirely new?</p>
  
  <p>Re-edit - I forgot that there is a loop in the main method. All hit detection should be in the main method also. I don't have the calls for Enemy stored as instances, how should I implement that exactly? Thank you for any assistance!</p>