Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Couple things:</p> <ol> <li>Your class is named <code>Bar</code> but you're calling it as <code>bar(a, b)</code>. Change that to <code>Bar(a, b)</code> to solve this problem.</li> <li>Classes need to inherit from something (if nothing, then <code>object</code>). You need <code>class Bar(object):</code></li> <li>Instance methods, in Python, are always supplied one parameter, which is the object itself, before other passed parameters. So your <code>def __init__(num1, num2):</code> should be <code>def __init__(self, num1, num2):</code>, and the same for <code>def foo()</code>.</li> <li>All of your instance variables need to be prefixed by <code>self.</code>. So <code>num1</code> should be <code>self.num1</code>, etc.</li> <li>The <code>^</code> operator is boolean XOR. Not sure if this is what you want, it's often confused with the power operator, <code>**</code>.</li> </ol> <p>Here's your example, cleaned up and fixed accordingly:</p> <pre><code>class Bar(object): num1, num2 = 0, 0 def __init__(self, num1, num2): self.num1, self.num2 = num1, num2 def foo(self): if self.num1 &gt; self.num2: print self.num1,'is greater than ',self.num2,'!' elif self.num1 is self.num2: print self.num1,' is equal to ',self.num2,'!' else: print self.num1,' is less than ',self.num2,'!' a, b = 42, 84 t = Bar(a, b) t.foo() t.num1 = t.num1 ^ t.num2 t.num2 = t.num2 ^ t.num1 t.num1 = t.num1 ^ t.num2 t.foo() </code></pre> <p>And the result:</p> <pre><code>42 is less than 84 ! 84 is greater than 42 ! </code></pre>
 

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