Unexpected __slots__ behavior

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Posts: 40
Joined: Sun Oct 29, 2017 5:14 pm

Unexpected __slots__ behavior

Post by manseekingknowledge » Fri Aug 02, 2019 4:38 pm

I'm on a crusade to reclaim RAM so I thought I would give slots a try. I was looking at the example here which says it shouldn't be possible to dynamically create a new attribute:

Code: Select all

class S(object):

    __slots__ = ['val']

    def __init__(self, v):
        self.val = v

x = S(42)

x.new = "not possible"
In MicroPython though this doesn't seem to be the case. In MicroPython the __dict__ variable of an instance is still created. The memory savings associated with __slots__ comes from the fact that (in CPython) the __dict__ variable isn't created. Is there some way to realize the potential memory savings typically associated with __slots__ in MicroPython?

User avatar
Posts: 3496
Joined: Fri Jul 18, 2014 8:01 am
Location: UK

Re: Unexpected __slots__ behavior

Post by pythoncoder » Sat Aug 03, 2019 6:19 am

This has been discussed here. As far as I can see it hasn't been implemented in the official build (unfortunately).
Peter Hinch

Post Reply