[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

meta protocol protocol

I brought up a topic earlier and am now taking a different slant.


I am thinking of the wisdom of having a protocol on announcing what
protocols you implement.


Python has many protocols that are now part of the language and I suspect
many more will arise. Python already has a series of variables stored in
classes or instances that store lists or dictionaries of THINGS and I won't
mention them in details as they are numerous and often mysterious. 


So I wondered what people thought of the general idea of adding one more
with some name like __PROTOCOLS__ that could be stored at something like the
class level.


Here are  examples of one person listing what they consider protocols:


When an object implements a protocol such as being callable, that generally
means they provide a method in the name __call__ which is simple enough to
check for if you know where to look. But the sequence protocol may require
quite a few such methods to be fully implemented and testing for dunders
len, getitem, setitem, delitem, reversed, contains, getslice, setslice, and
delslice may be a bit much.


So what if the powers that be that determine the fate of python come up with
a meta protocol. All protocols would be given a NAME for official purposes.
The two examples might become "callable" and "sequence" and the meaning of
saying you properly provide a protocol would be well documented.


So if someone creates an object, part of the process might be to add entries
for each protocol you declare you properly support to something like the
__PROTOCOLS__ variable. How to do so is an implementation decision but it
could be as easy as adding a method to the top class "object" than any
subclass can inherit and use to declare one or more protocols in use. You
might also add another method like has_protocol(name) that returns True if
it is registered.


As a programmer, you might no longer need to know the details to see if
something is in some category. Rather than checking for the presence of
various dunderheads, you simply ask if a protocol is implemented and then
use it.


I am not sure anything like this is wanted or practical and am asking if
people have already suggested something along these lines or whether it
would even be something worth doing or practical.


I can think of many complications starting with what happens if someone lies
and continuing with what happens if a class inherits from multiple other
classes but some methods are over-ridden or no longer the first one found in
the MRO search.


Of course, it would also be nice if there was also a tool that could be used
by developers to validate claims to implement a protocol before run-time
either in a shallow way (the right names are defined) or a deeper way by
exercising the functionality to see it does what is expected. That, too,
leads to me wondering how this could work as the above reference suggests
the copy protocol contains both a __copy__() and a __deepcopy__() and I can
imagine having just the first.


When I say PROTOCOL, I mean it in a very broad sense. For example, what
makes an object type IMMUTABLE? What makes the contents of an object
INVISIBLE as in you use techniques so the only way to gain access to the
hidden internals of an object is through various kinds of proxies that keep
you from actually seeing what is going on. Such aspects may not have an
exact protocol but if you want to use something as a key in a dictionary
maybe it will be happy to allow it if it thinks you are of an immutable
type. Other functionality may refuse to work with objects not deemed
invisible for privacy concerns.


Again. Not a serious proposal just wondering if my thought process is flawed
deeply or am I just shallow.