Implementing/Overriding operators

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
dastultz
Posts: 20
Joined: Mon Jan 11, 2016 2:56 am

Implementing/Overriding operators

Post by dastultz » Fri May 27, 2016 1:46 am

Hello,

I've got a project where I am implementing/overriding operators for a class. Consider this entered at a CPython prompt:

Code: Select all

>>> class Monkey:
...     def __abs__(self):
...         return "Fred"
... 
>>> m = Monkey()
>>> abs(m)
'Fred'
The same code on a Pyboard yields:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert Monkey to int
I'm on MicroPython v1.8-76-gca63c77 on 2016-05-15; PYBv1.0 with STM32F405RG

Any ideas? I think I've got problems with other operator overloads as well. I can do what I need with regular methods on my objects but I thought it would be nice to implement operators for my base class which is really just a wrapper around numbers.

Thanks!

/Daryl
Please don't forget to put the code in the appropriate bbcode - platforma

PinkInk
Posts: 61
Joined: Tue Mar 11, 2014 3:42 pm

Re: Implementing/Overriding operators

Post by PinkInk » Thu Jun 09, 2016 9:36 am

Reference https://github.com/micropython/micropyt ... ifferences, particularly """3.MicroPython does not implement complete CPython object data model, but only a subset of it.""". Micropython is "micro" ... just how much of Python it crams into a miniscule package is a miraculous testament to just how clever Damien, Paul & Co. are.

Every time I hit these firewalls I find that stepping back and reassessing what I was trying to achieve and how I was trying to achieve it inevitably leads to two conclusions;

- what has, and hasn't, been included in micropython has been very carefully considered
- in every case; there has been a better, neater, more concise, micro and pythonic way to achieve what I wanted to do, it just wasn't the first approach that popped into my head - hitting the firewall forced me to rethink and allowed me to identify "the better" way and learn from it.

Post Reply