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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:57:51+00:00 2026-06-14T15:57:51+00:00

This program modifies objects of the class myclass _x and _y, but I don’t

  • 0

This program modifies objects of the class “myclass” _x and _y, but I don’t pass it as a parameter to the function try_block. How do the objects get modified?

class AddSub:
    def _init_(self): #how do default parameters work? 
        self._x, _y

    def set_x(self, num):
        self._x = num

    def set_y(self, num):
        self._y = num   

    def add(self):
        return self._x + self._y

    def sub(self):
        return self._x - self._y

def try_block():
    try:
        ch = int(input("type 1 to add, and 2 to subtract: "))

        myclass.set_x(int(input("enter an x value: "))) #how does myclass get modifed?

        myclass.set_y(int(input("enter a y value: ")))

        return ch

    except ValueError:
        print("Invalid entry.")

        ch = try_block()

        return ch


myclass = AddSub()

choice = try_block()

if choice == 1:
    print(myclass.add())

elif choice == 2:
    print(myclass.sub())
  • 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-14T15:57:52+00:00Added an answer on June 14, 2026 at 3:57 pm

    Before I go into answering your question, I want to mention some things about terminology. Your myclass value is an instance, not a class. You do have a class in your code, named AddSub. When you called AddSub() you created an instance of that class. It’s important to learn the right terminology for things like this, so you can ask good questions and understand the answers you get back.

    Your code comes close to working because you’re saving an instance of the AddSub class to a global variable named myclass. Later, you call some methods on that global variable from the try_block function. This is legal in Python, though generally not recommended.

    Instead, you should pass the object as an argument:

    def try_block(value):
        try:
            value.set_x(whatever())
        except ValueError:
            pass
    

    You’d call it by passing an instance of your AddSub class to the function:

    myAddSub = AddSub() # create the instance
    try_block(myAddSub) # pass it to the function
    

    This is much nicer because it doesn’t depend on a global variable having a specific value to work and you can call it with many different AddSub instances, if you want.

    One part of your code that’s currently broken is the AddSub class’s constructor. There’s no need to declare variables. You can just assign to them whenever you want. If you want to have default values set, you can do that too:

    def __init__(self):
        self._x = 0
        self._y = 0
    

    If instead you want to be able to set the values when you construct the object, you can add additional parameters to the __init__ method. They can have default values too, allowing the caller to omit some or all of them:

    def __init__(self, x=0, y=0):
        self._x = x
        self._y = y
    

    With that definition, all of these will be valid ways to construct an AddSub instance:

    a = AddSub()     # gets default 0 value for both _x and _y
    b = AddSub(5)    # gets default 0 value for _y
    c = AddSub(y=5)  # gets default 0 value for _x
    d = AddSub(2, 3) # no defaults!
    

    Finally a point that is mostly independent of your main question: Your try_block function is both poorly named, and implemented in a more complicated way than necessary. Instead of being recursive, I think it would make more sense as a loop, like in this psuedocode version:

    def exception_prone_task():
        while True: # loops forever, or until a "return" or "break" statement happens
             try:
                 result = do_stuff_that_might_raise_an_exception()
                 return result # you only get here if no exception happened
    
             except WhateverExceptions as e:
                 report_about_about_the_exceptions(e)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've asked this question recently a few different ways, but don't get an answer
I am supposed to modified this Sierpinski's triangle program to count the number of
This program runs just fine, but for some reason when I ask for an
this program is for class to display the database in Listview.i want to add
I get this error sometimes, Catchable fatal error: Object of class stdClass could not
I have a class that would normally just generate factory objects, however this class
This program builds a dictionary out of a list based on what two index
This program returns: package main import ( flag fmt ) func main() { num_agents
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word

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.