You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Standardizing on an (extension) method to reinitialize an object inplace #4331
It's well-known that a good way to fight memory fragmentation is preallocation and reuse of buffers objects. "And reuse" clause should not be underestimated, because that's the real action which happens there - a preallocated object is reused, using inplace operations. This is easy for objects like buffers, but this idea can be generalized beyond them. One case is being talked at #2552 (comment) - reusing memoryview with another buffer. But it goes beyond that still. E.g. reusing a closed socket to connect again without allocating a new instance. Reusing uzlib.DecompIO to decompress another stream. Etc.
P.S. I understand this has zero chance being implemented in the mainline, I'm going to implement this in my fork. Posting here for wider community review.
So, neat idea is tore purpose __call__ method for this, leading to "nice" symmetry call a class vs calling an object, where calling a class (allocates and) initializes a new object, where calling an object reinitializes it:
However, this won't work for objects which have __call__ already.
And fairly speaking, this would be called nice strange symmetry, and may confuse novices, and then with some probability and in some contexts experienced user too.
Then an obvious is to not invent magic syntaxes, but just add a normal method. Name .init() comes to mind. That's a pretty natural name, but it has drawback that it may exist for an object, just the same as __call__. However, objects in the immediate scope of this proposal don't have such. Actually, in MicroPython core API, only machine module classes have it, but semantics nicely matches this proposal - there, .init() is exactly used to reinitialize an object.
And pretty match, this proposal talks about adding metasemantics to an .init(), that the method can be used for reinitialization of object, not just initialization. It doesn't dictate other aspects of .init().
So again, the problem are objects which already have .init() in their API (say, coming from CPython). But then this proposal talks only about:
Allowing that .init() method to be called more than once - if that makes sense for that particular .init() method, i.e. it allows to reinitialize enough of object state for the reuse of object in other context.
And if that's not true (existing .init() does not allow to reinitialize enough of object state for the reuse of object in other context), the we just add another signature for .init() to cover the object reuse case, leaving the original .init() alone.
The other alternative is to add a complete adhoc method like .reuse(...). But as argued in the previous comment, implied semantics of .init() is already pretty close to allow reuse without inventing weird-looking methods, and it should be always possible to extend any existing .init() method to pretty naturally add a reuse case.
Activity
pfalcon commentedon Dec 1, 2018
So, neat idea is tore purpose
__call__method for this, leading to "nice" symmetry call a class vs calling an object, where calling a class (allocates and) initializes a new object, where calling an object reinitializes it:However, this won't work for objects which have
__call__already.And fairly speaking, this would be called
nicestrange symmetry, and may confuse novices, and then with some probability and in some contexts experienced user too.pfalcon commentedon Dec 1, 2018
Then an obvious is to not invent magic syntaxes, but just add a normal method. Name
.init()comes to mind. That's a pretty natural name, but it has drawback that it may exist for an object, just the same as__call__. However, objects in the immediate scope of this proposal don't have such. Actually, in MicroPython core API, onlymachinemodule classes have it, but semantics nicely matches this proposal - there,.init()is exactly used to reinitialize an object.And pretty match, this proposal talks about adding metasemantics to an
.init(), that the method can be used for reinitialization of object, not just initialization. It doesn't dictate other aspects of.init().So again, the problem are objects which already have
.init()in their API (say, coming from CPython). But then this proposal talks only about:pfalcon commentedon Dec 1, 2018
The other alternative is to add a complete adhoc method like
.reuse(...). But as argued in the previous comment, implied semantics of.init()is already pretty close to allow reuse without inventing weird-looking methods, and it should be always possible to extend any existing.init()method to pretty naturally add a reuse case.pfalcon commentedon Dec 1, 2018
Comments are welcome. I'm already starting to implement this in my fork.
Merge pull request micropython#4331 from dhalbert/tilegrid-transpose-fix
dpgeorge commentedon May 1, 2026
While this is a neat idea, I think it will diverge too much from CPython and make it harder to write code compatible with normal Python.