using class sample

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
amadeus
Posts: 1
Joined: Sun Apr 11, 2021 8:23 pm

using class sample

Post by amadeus » Sun Apr 11, 2021 8:50 pm

I am using rp2-pico-20210411-unstable-v1.14-152-g6f06dcaee.uf2 on my Raspberry pi Pico board. I tried the following class samples in 'Thonny 3.3.6' with the following error message:

>>> %Run -c $EDITOR_CONTENT
This is a static method
I can do something with self.
Traceback (most recent call last):
File "<stdin>", line 27, in <module>
TypeError: can't convert Data to int:

line 27 is: print(hex(d))

---------------------------------------------------------------------------------------------------------------------
def static_method(self):
print("This is a static method")

def object_method(self):
print("I can do something with self.")

p = Person()

Person.static_method(p)
p.object_method()

#____________________________________________________________

class Data:
id = 0

def __index__(self):
print('__index__ function called')
return self.id


d = Data()
d.id = 100
print(hex(d))

------------------------------------------------------------------------------------------
All works fine with Python versions 3.7 - 3.9 . What is the problem with MicroPython or my code?

Post Reply