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 6689407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:30:56+00:00 2026-05-26T05:30:56+00:00

I have this code I am trying out and I can’t figure out how

  • 0

I have this code I am trying out and I can’t figure out how to modify the instance attributes when I use them as parameters when calling a method.

class Portfolio(object):
    def __init__(self):
        self.qtyA = 50
        self.qtyB = 0
        self.qtyC = 0
        self.cash = 0  

    def __str__(self):
        return ("%d %d %d %.2f")%(self.qtyA, self.qtyB, self.qtyC, self.cash)

    def buy(self, stockQty, qtyToBuy, price, commission=20):
        stockQty += qtyToBuy
        print stockQty
        buyTransaction = price * qtyToBuy
        self.cash -= buyTransaction + commission

>>> p = Portfolio()
>>> p.buy(p.qtyA,10,25)
60
>>> print p
50 0 0 -270.00

What seems to be happening is that a new variable stockQty is being created when buy is called. I would expect that a reference to the attribute qtyA would be passed instead of the value. This problem might be related to this question: How do I pass a variable by reference?

How can I work around this issue?

  • 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-26T05:30:56+00:00Added an answer on May 26, 2026 at 5:30 am

    This problem might be related to this question: Python: How do I pass a variable by reference?

    It is.

    How can I work around this issue?

    You need to pass a value that can be modified, and modify it. Numbers cannot be modified: a can be changed from referring to 23 to referring to 42 instead, but you cannot cause 23 to become 42 or vice-versa.

    In your case, the natural way to do this is to also notice the other silly thing you’re doing – using a bunch of related, similarly-named variables in parallel – and fix that as well, by replacing them with a sequence. The list type is a modifiable sequence. You need to pass the list itself instead of just a numeric quantity, and indicate which element of the list to replace. Except you don’t actually need to pass the list, because it’s already a part of self.

    class Portfolio(object):
        def __init__(self):
            self.quantity = [50, 0, 0]
            self.cash = 0  
    
        def __str__(self):
            return ("%d %d %d %.2f")%(self.quantity[0], self.quantity[1], self.quantity[2], self.cash)
    
        def buy(self, stockToBuy, amountToBuy, price, commission=20):
            self.quantity[stockToBuy] += amountToBuy
            cost = price * amountToBuy
            self.cash -= cost + commission
    

    For a more flexible solution, you might consider the idea of having an association between stock names and quantities of stock – after all, who knows what stocks the client might want in the future. We can do this simply by using a dict instead.

    (A constant ‘commission’ cost is also unrealistic; a percentage makes more sense.)

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

Sidebar

Related Questions

I have this code in my ASP.NET application written in C# that is trying
I'm extremely new to JS and have this code that I'm trying to tweak.
I am trying to compare two dates. I have this code which I thought
I have this piece of code I'm trying to get to display but no
I have got this code and I am trying to understand the convention followed,
I have this php code, with which I am trying to generate a popup
I am trying to enable mod_deflate. I have Apache 2.0+ and tried this code
All, I am trying to code a Connect4 game. For this, I have created
I'm trying to get the direction property for the element. I have this code:
I have this code i'm trying to convert from C# to VB.net: public object

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.