6

I'm using Python. I've read a bit about this and can't seem to wrap my mind around it. What I want to do is have a class called Potions with various potion objects in it. For now there's one potion, a simple HealthPotion. I want potions to be stackable in inventories and shop stocks. So I need an instance of the potion amount for an inventory and an instance for each shop stock that carries potions. The potion amount would be dynamic, for buying/selling and looting of potions. If someone could provide a basic explanation or examples that would be great.

Here's a snippet of what I have:

class Potion(Item):
    def __init__(self, name, desc, val, amt, type, effect, bound):
        Item.__init__(self, name, desc, val, amt, type, effect, bound)

        self.name = name
        self.desc = desc
        self.val = val
        self.amt = amt
        self.type = 0 #Restorative
        self.effect = effect

   def use(self, you):
    #Use health potion
        you.hp_current += self.effect
        you.displayStats()

#Format: Name, Description, Value, Amount, Type, Effect, Bound
HealthPotion = Potion('Health Potion', 'Restores 10 hit points when consumed', 10, 0, 
0, 10, 0)

Ideally the default amount would be set to 0 and I'd be able to declare how much a certain shop would start with in their stock. The inventory and shop stock is set up as an array that items are appended and removed to/from. I think I've got the logic down for how this would work, I just am having trouble with instancing the amounts.

EDIT: This is part of what I have in a buy method to see what would happen without using instances. It's pretty ugly and I discerned that you.inventory.y.amt will not work. y being the selected item from the list of items that is displayed in a "shop."

            x = selection - 1 #Item menu starts at 1. But arrays start at 0. So it takes user input and subtracts 1 to match the array.
            y = self.stock[x]

            if y.val <= you.coin:
                if y.amt == 0:
                    you.inventory.append(y)
                    you.inventory.y.amt += 1
                else:
                    you.inventory.y.amt += 1;

                you.coin -= y.val
                self.funds += y.val

                if self.stock.y.amt > 1:
                    self.stock.y.amt -= 1
                else:
                    self.stock.y.amt -= 1
                    self.stock.pop(x)

I've looked at examples like this:

class foo: 
a = 1 
i = foo() 
foo.a => 1 
i.a => 1 
i.a = "inst" 
foo.a => 1
i.a => "inst"

I'm wondering if I don't just create a second HealthPotion object, but that doesn't sound right to me. This example leads me to think otherwise. Maybe I just don't understand instancing.

"This would create first object of Employee class"

emp1 = Employee("Zara", 2000)

"This would create second object of Employee class"

emp2 = Employee("Manni", 5000)

Thanks!

TechnoCat
  • 145
  • 2
  • 2
  • 7
  • 2
    So, what exactly is your problem? What have you tried to solve your problem? – Marcin Feb 27 '12 at 15:33
  • 1
    Not a solution to your problem, but it should not be necessary for you to set your member variables in the potion init if you are calling your superclass init. – Marcin Feb 27 '12 at 15:34
  • You probably want to use named keyword arguments instead of a comment when constructing. (`health_potion = Potion(name="Health Potion", desc="Restores...", ...`) – Daenyth Feb 27 '12 at 15:36
  • Thanks for the tip Daenyth. @Marcin - I just want a simple example of how to set up instances in this sort of scenario. The way I'm trying to set it up now, without instances, is syntactically wrong and the logic behind it isn't going to work at all because the amount attribute would be shared between stocks and inventory and I can't really specify where to add/remove a potion. If they're instanced, I believe I should be able to say "player.potion.amt +=1, shop.potion.amt -+ 1" That's some super pseudo code right there. – TechnoCat Feb 27 '12 at 15:42
  • 1
    Don't ask for examples. Show us your attempt to achieve the task, and give a proper description of the task. Explain why your attempt does not achieve the intended result. Put all of that in your question, not in the comments. – Marcin Feb 27 '12 at 15:44
  • 1
    Also, if any of the answers represents a complete resolution of your issue, you should accept the best one. If not, do update your question to clarify further. – Marcin Feb 27 '12 at 16:38

2 Answers2

18

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:

class Person(object):
    def __init__(self, name):
        self.name = name

    def speak(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.

Katriel
  • 120,462
  • 19
  • 136
  • 170
  • Thank you, thank you, thank you. This makes tons more sense. You have assisted in untangling the mess that was my mind. :] I do believe my understanding of how to do what I wanted to do was off by a bit. Thanks! – TechnoCat Feb 27 '12 at 16:14
  • @SarahMac no problem, glad to help =) do let me know if you want a hand working out the new inheritance structure. – Katriel Feb 27 '12 at 19:28
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

class HealthPotion(Potion):
    def __init__(self, name="Health Potion", effect=10):
        super(HealthPotion, self).__init__(name, "Restores %d points of health" % (effect, ), effect, 0, 0)
    def use(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:

def stack_size(item, inv):
    "The number of items that will stack with item in inv"
    return len([i for i in inv if item.stacks_with(i)])

def stacked_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 in range(0,len(inv)) 
                  if not stack_size(inv[i], inv[0:i])]
    return stacks
tobyodavies
  • 27,347
  • 5
  • 42
  • 57
  • This was also very helpful, thank you much. I originally had my HealthPotion named something like weak_potion but ended up changing the name to test something with the use method and never changed it back. I'll pay more attention to my naming conventions. Thank you! – TechnoCat Feb 27 '12 at 16:15
  • @SarahMac added how I would implement an inventory – tobyodavies Feb 27 '12 at 16:57