[Swlug] Quick Pythom question.

Dave Cridland dave at cridland.net
Sat Jul 16 19:18:33 UTC 2022


Hey Rhys,

You've found a bit of sample code for a function, but then used it as a
method.

Methods get passed an additional argument on call, that of the object
itself, usually named as `self`.

So just adding that argument in is sufficient:

def L76X_GetMoreData(self, GPS_Speed, SPD_Direction):

But if you're passing in parameters from itself anyway, so you could remove
those and reference self.GPS_Speed anyway, but then, you're not actually
using those parameters, so I kind of wonder why.

I can suggest downloading JetBrains's Python IDE, PyCharm, which'll help
identify problems like these as you type. It's not open source, but it's a
key part of my professional toolkit.

Dave.

On Wed, 13 Jul 2022 at 23:29, Rhys Sage via Swlug <swlug at mailman.lug.org.uk>
wrote:

> I found a lot of the demo code for the L76D doesn't actually work. Rather
> than modify it, I'm trying to add my own sections. I'm struggling a bit on
> my own with the Python so here's what I'm doing....
>
> In Main.py I'm using this...
>
>     x.L76X_GetMoreData(x.GPS_Speed,x.GPS_Direction)
>     print('Speed ', x.GPS_Speed, ' Direction ', x.GPS_Direction)
>
> in the L76X code I'm using this:
>
>     def L76X_GetMoreData(GPS_Speed, GPS_Direction):
>         #data = GPS_DATA
>         for i in range(0, BUFFSIZE-71):
>             if(ord(data[add]) == 36 and ord(data[add+1]) == 71 and
> (ord(data[add+2]) == 78 \
>             or ord(data[add+2]) == 80) and ord(data[add+3]) == 82 and
> ord(data[add+4]) == 77\
>             and ord(data[add+5]) == 67):
>                 print("Bloody Hell, it works!")
> #        self.GPS_Speed = 0
> #        self.GPS_Direction = 0
>         #self.GPS_DATA = ""
>
> But I'm getting errors...
>
> Traceback (most recent call last):
>   File "<stdin>", line 56, in <module>
> TypeError: function takes 2 positional arguments but 3 were given
>
> I am mightily confused by this. What's going on? What am I
> misunderstanding?
>
> Rhys Sage
>
> --
> Swlug mailing list
> Swlug at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/swlug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/swlug/attachments/20220716/3577a49a/attachment.htm>


More information about the Swlug mailing list