Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>List contains: [A1, A2, A3, B1, B2, B3, C1, C2, C3] Line 30: TypeError: choose_last() takes exactly 1 arguments (0 given) </code></pre> <p>This is because you call choose_last() for a class, not object. In Python all methods called for instance of a class (for object) have an implicit first argument: <code>self</code></p> <p>In other words, calling <code>P1.choose_last()</code> is exactly like:</p> <pre><code>Position.choose_last(P1) </code></pre> <p>Interpreter wants you to pass one parameter, <code>self</code>, or to call the method for the object, not class. Of course calling methods for objects is the preferred OOP way to do, because you can write code not knowing exact class of the object - objects you manipulate can be <a href="http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29#Subtyping" rel="nofollow" title="polymorphic">polymorphic</a>.</p> <p>From the Python documentation:</p> <blockquote> <p>Actually, you may have guessed the answer: the special thing about methods is that the object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument.</p> </blockquote> <p>As for the retrieving the last element from the list, by convention negative indexes mean 'I'm counting from the end':</p> <pre><code>def choose_last(self): return self.xy[-1] </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