2

So, I have looked into this question and haven't gotten what I see to be a solid answer, or maybe I just am lacking an understanding of this. Essentially I want to know:

A. Is it bad practice to have many instances of the same class?

B. What is a way to get rid of an overwhelming amount of instances without the program exiting?

Let me explain. Say I want to write a Zero Person RPG that is running in the background consistently. So I create an Enemy class for the Hero to slay.

class Enemy:
    # Attr = Stats held in a dict
    def __init__(self, attr={}):
         self.attr = attr

A simple example. Is there an alternative to having to do the following hundreds of times?

giant = Enemy({'atk': 10, 'def': 5})
poltergeist = Enemy({'atk': 7, 'def' 8})
...

Or is this seen as the pythonic way?

guest1234
  • 208
  • 2
  • 5

1 Answers1

1

No, it is not a bad practice to have many instances of the same class. When working with ORMs, you'll be working with a lot of objects, and it is totally fine as long as you are not being redundant and your use case needs the instances, and deleting the objects when you do not need them anymore.

Various ways of deleting and object are explained in this answer.

This is another answer which showcases use of with statement to manage objects contextually.

Community
  • 1
  • 1
xssChauhan
  • 2,728
  • 2
  • 25
  • 36