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)