bbcmicrobit/micropython

Error in error

FrankSAURET opened this issue · 4 comments

Hi,
number of arguments returned is false. For example in this code :

from microbit import * 

display.set_pixel(3,2,4,9)

The error is :
"function takes 4 positional arguments but 5 were given" but must be :
"function takes 3 positional arguments but 4 were given"

Hi @FrankSAURET

I agree that this is a bit confusing, but it is the expected Python behaviour, you can try this on a CPython or MicroPython REPL and get the same result:

$ python
Python 3.7.12 (default, Feb 16 2022, 19:03:18)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo():
...     def __init__(self, first_arg):
...             pass
...
>>> Foo(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() takes 2 positional arguments but 3 were given
>>>

So as you can see, the extra "hidden" argument here is self, as display is an instance of the MicroBitDisplay class, and self is automatically passed as the first argument.

This stack overflow covers the how and why of self: https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self

I hope that clarified things.

Thank's for this explanation.

that this is a bit confusing,
For expiramented user just a litle bit but for my beginners students this is very confusing and they always try to add one more parameter.

For interest, the beta version of the Python Editor at https://python.microbit.org/v/beta does some up-front checking of Python code and shows errors in the way you might expect:

Screenshot2022_06_24_164459

Nice job, thanks for showing it to me.
I was asking the question to do this same fix in the vscode extension I'm coding at https://marketplace.visualstudio.com/items?itemName=electropol-fr.microbit-explorer. And I made the change yesterday.
I note the idea of help on the side that I would probably add.