I'm not that familiar with Python, and more used to 2 than 3. I'm mostly a C++ engineer with a lot of experience in that.
I have some work to do with MicroPython targeting ESP32 and I'm trying to get up to speed on pythonic techniques for best practice.
I have a couple of specific questions:
1. I've been looking for the correct way to do dependency inversion, and it seems to involve super(), however super and MRO are incomplete in MP, on ESP32 anyway. What is the most robust and simple way to achieve this?
2. I tried some different ways to create read only objects using descriptor mechanisms, such as overriding __setattr__, but also fell upon stony ground. I'd be interested in any general observations on these methods in Python 3 and MicroPython.
3. I'm looking for a simple singleton (an antipattern where I come from ). I'd prefer one that behaves like a class, but this one I found on this forum (viewtopic.php?t=5033) looks useful. I'd be interested to hear any suggestions.
Code: Select all
def singleton(cls): instance = None def getinstance(*args, **kwargs): nonlocal instance if instance is None: instance = cls(*args, **kwargs) print(instance, id(instance)) return instance return getinstance @singleton class MyClass1(): def method(self): pass