Python Creating Multiple Instances For A Single Object/class
Solution 1:
I think you may have a slightly misguided concept of how classes and instances work. It might make more sense if you think about people instead.
Suppose we want to model people in our class hierarchy. For now, a person has to have a name, and if you ask them to speak they say their name:
classPerson(object):
def__init__(self, name):
self.name = name
defspeak(self):
print"Hi! My name is {self.name}.".format(self=self)
Then an instance of Person
is, well, a person! For example:
>>>basil = Person("Basil")>>>polly = Person("Polly")>>>>>>basil.speak()
Hi! My name is Basil.
>>>polly.speak()
Hi! My name is Polly.
>>>basil == polly
False
So an instance isn't a kind of person -- it really is just a person.
Can't you have a class whose instances are themselves classes, I hear you ask? Yes, of course you can! It's called a metaclass and is a very powerful tool in certain circumstances. Not, however, these.
Now, if you look at your situation, do you see the difference? A HealthPotion
isn't a particular potion (say, this one in my pocket) -- it's a kind of potion. And the way we express that relationship is by class inheritance: define a new class HealthPotion
which inherits from Potion
. You can then have instances of these (in my pocket, in a store, wherever). If you want to use a potion, you use a specific one i.e. an instance of the class.
Solution 2:
The code you have there uses very confusing naming conventions, which I think is causing you confusion --- HealthPotion is not a class, it is an instance, but the CamelCase name suggests it is a class in python.
To have multiple health potions using your potion class, simply do
health_potion_1 = Potion("Health Potion", ...)
health_potion_2 = Potion("Health Potion", ...)
foobar_potion_1 = Potion("Foobar Potion", ...)
#...
Though this is pretty poor style, what you probably want is to have a way to easily create health and similar potions, with the same properties and potions with different effects
To do this you should have
classHealthPotion(Potion):def__init__(self, name="Health Potion", effect=10):
super(HealthPotion, self).__init__(name, "Restores %d points of health" % (effect, ), effect, 0, 0)
defuse(self, you):
you.hp_current+=self.effect
If you want to have multiple items in an inventory it would be simplest to simply have a list (or set or some collection) for your inventory and have multiple instances in the list, e.g.
inventory = [HealthPotion(), HealthPotion()]
If you want to do stacking, I still think that is a function of the inventory, not the item (beyond a item.stackable
member) so I would have an Inventory
class which handles collections of object, be that the contents of a person, a chest or a shop. a simple implementation would be a wrapper around
inventory = [(HealthPotion(), 2)]
where any identical items are represented as a pair of the item and the amount
Alternatively, it is pretty easy to transform the former into the latter if you have a stacks_with
method:
defstack_size(item, inv):
"The number of items that will stack with item in inv"returnlen([i for i in inv if item.stacks_with(i)])
defstacked_inventory(inv):
# if there is no stackable pair in the first i items# (i.e. the stack size for that item is 0 in inv[0:i]),# then the 'i'th item is a new stack,# with stack_size(inv[i], inv) items in it
stacks = [ (inv[i], stack_size(inv[i]))
for i inrange(0,len(inv))
ifnot stack_size(inv[i], inv[0:i])]
return stacks
Post a Comment for "Python Creating Multiple Instances For A Single Object/class"