Pre-PEP: Safe Parallel Python

I’ll leave it to others to discuss the benefits of this proposal, but I have a few questions about the API.

As far as I can tell, there are four new proposed proprerties, all of which are special names/dunders:

  • object.__freeze__()
  • object.__protect__()
  • object.__mutex__
  • object.__shared__

From the python docs, “A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names.” Whilst there are certainly plenty of special names (mostly non-methods) which don’t correspond to operators (e.g. function.__closure__, object.__annotations__, function.__doc__, __slots__) , these are usually either very internal (e.g. __closure__) or have some other way to access them (e.g. get_annotations() or help(function)). Not to say that there aren’t exceptions to this (__slots__), but in general, special names are special for a reason.

However, when looking at the example, there seemingly isn’t a ‘standard’ way to access these properties.

If I’m learning Python and trying to make an object immutable to share it between threads, I’m expecting to find some kind of freeze() builtin, or a threading.freeze() function, and am probably not going to reach for a specially named attribute on the object itself, which seems like it shouldn’t be messed with (object.__freeze__()).

Particularly, it can make a perfectly normal function where I’m just trying to mutate some variables look very internal:

def func1(a, b):
    with a.__mutex__ + b.__mutex__:
        some_mutation_here()

Or, I might assume that object.__protect__() is meant to be overwritten/overwritable, like the special methods for operators. Now, maybe this is the intent? But it isn’t clear in the PEP.

object.__shared__ seems more like the kind of implementation detail that you would leave as a dunder though, so I don’t mind this.

Anyway, those are my thoughts. Have you had any ideas yourself, as to APIs for accessing the added special attributes/methods?

6 Likes