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

  • SEARCH
  • Home
  • 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 7855643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:26:49+00:00 2026-06-02T20:26:49+00:00

The outcome is not what I expected. When guya(an object) gets his lemon(also object)

  • 0

The outcome is not what I expected. When guya(an object) gets his lemon(also object) from guyb, both of them had their lemons increased and then decreased by 1, from what it seems. But if you uncomment the only commented line from this program, I tried to assign guyc lemon’s count to 1000, but that only set everyone’s lemon.quantity to 1000. What’s going on?

My purpose of the getlemon, in class slasher, function was to decrease the quantity of the target’s lemon by 1, and increase the self quantity of lemon by 1, but that failed. What did I do wrong? And when I tried to assign guyc.lemon.quantity to 1, what happened?

class item():
  def __init__(self, x = 0): self.quantity = x
  def set_quantity(self, newquantity): self.quantity = newquantity
class slasher():
  health = 10
  lemon = item(10)
  def getlemon(self, target):#target is the placeholder
    target.lemon.quantity -= 1
    self.lemon.quantity += 1
    target.health -= 1
guya = slasher()
guyb = slasher()
guyc = slasher()
guya.getlemon(guyb)
#guyc.lemon.quantity = 1000
def printit():
  print("guya's lemon count:", guya.lemon.quantity)
  print("guyb's lemon count:", guyb.lemon.quantity)
  print("guyc's lemon count:", guyc.lemon.quantity)
  print("guyb's health:", guyb.health)
printit()
  • 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-06-02T20:26:50+00:00Added an answer on June 2, 2026 at 8:26 pm

    lemon = item(10) runs exactly once, when the class is defined. So no matter which instance – you will always access the same object.

    To avoid this, create the object in the constructor:

    def __init__(self):
        self.lemon = item(10)
    

    You might want to do the same for health – however, it’s not necessary in this case since as soon as you assign something to obj.health it will be set on the instance (numbers are immutable.)

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

Sidebar

Related Questions

Expected Outcome: Successful installtion of Plone 4.1.4 Actual Outcome: Installation fails with: libxml2: cmmi
Firstly could you view my outcome http://jsfiddle.net/RXnKR/ From this I decided as you click
Is there any way to calculate the largest outcome from an Rijndael encryption with
Below is my javascript code snippet. Its not running as expected, please help me
I am not 100% sure the outcome that would occur in my current setup
I am trying to test that a particular method throws an expected exception from
The exact structure of the struct is not important. From what I gather the
Problem: The following shell script code does not produce the expected result: # MYSQL,
Two questions. 1) I understand this must be an expected outcome but maybe someone
My 7 year old would like to learn, how to program? (his idea not

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.