Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Before I go into answering your question, I want to mention some things about terminology. Your <code>myclass</code> value is an instance, not a class. You do have a class in your code, named <code>AddSub</code>. When you called <code>AddSub()</code> 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.</p> <p>Your code comes close to working because you're saving an instance of the <code>AddSub</code> class to a global variable named <code>myclass</code>. Later, you call some methods on that global variable from the <code>try_block</code> function. This is legal in Python, though generally not recommended.</p> <p>Instead, you should pass the object as an argument:</p> <pre><code>def try_block(value): try: value.set_x(whatever()) except ValueError: pass </code></pre> <p>You'd call it by passing an instance of your AddSub class to the function:</p> <pre><code>myAddSub = AddSub() # create the instance try_block(myAddSub) # pass it to the function </code></pre> <p>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 <code>AddSub</code> instances, if you want.</p> <p>One part of your code that's currently broken is the <code>AddSub</code> 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:</p> <pre><code>def __init__(self): self._x = 0 self._y = 0 </code></pre> <p>If instead you want to be able to set the values when you construct the object, you can add additional parameters to the <code>__init__</code> method. They can have default values too, allowing the caller to omit some or all of them:</p> <pre><code>def __init__(self, x=0, y=0): self._x = x self._y = y </code></pre> <p>With that definition, all of these will be valid ways to construct an <code>AddSub</code> instance:</p> <pre><code>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! </code></pre> <p>Finally a point that is mostly independent of your main question: Your <code>try_block</code> 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:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload