Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7553413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:02:49+00:00 2026-05-30T11:02:49+00:00

I’m using Python. I’ve read a bit about this and can’t seem to wrap

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:02:50+00:00Added an answer on May 30, 2026 at 11:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.