PEP 472 -- Support for indexing with keyword arguments
Hello!
I'm not sure I'm addressing the right audience here, so please direct me
to the appropriate channel if that's the case...
My name is Andras Tantos and I'm working on a Python library desscribing
HW designs. I came across this problem of __getitem__ and co. not
supporting kwargs. Apparently this extension was proposed and rejected
as PEP 472.
Apart from my use-case, which is arguably a corner-case and not worth
modifying the language for, I believe there are two important use-cases
that are worth considering with the latest improvements in the language:
1. With the recent type-hint support, the feature could be made way more
descriptive if this PEP got implemented.
For example, instead of doing the following:
def func(in: Dict[str, int])
one could write:
def func(in: Dict[key=str, value=int])
2. It would also make 'generic classes' much cleaner to implement,
similar to the way type-hints look. Consider the following code:
class_Generic(object):
Specializations = []
@classmethod
def__getitem__(cls, *args):
name = f"Generic_{len(cls.Specializations)}"
Specialized = type(name, (cls,), {"specials": tuple(args)})
cls.Specializations.append(Specialized)
returnSpecialized
def__init__(self, value= None):
self.value = value
def__str__(self):
ifhasattr(self, "specials"):
return(f"[{type(self)}- "+ ",".join(str(special) forspecial
inself.specials) + f"] - {self.value}")
else:
return(f"[{type(self)}- GENERIC"+ f"] - {self.value}")
Generic = _Generic()
#g = Generic() - fails because of no specialization is given
s1 = Generic[12]()
s2 = Generic[42]("Hi!")
print(s1)
print(s2)
Running this simple example results in:
python3 -i python_test.py
[
participants (24)
-
Alex Hall
-
Andras Tantos
-
Antoine Pitrou
-
Caleb Donovick
-
Christopher Barker
-
David Mertz
-
Dominik Vilsmeier
-
Eric V. Smith
-
Gerrit Holl
-
Guido van Rossum
-
Joao S. O. Bueno
-
Jonathan Fine
-
Jonathan Goble
-
MRAB
-
Neil Girdhar
-
Paul Moore
-
Paul Sokolovsky
-
Rhodri James
-
Ricky Teachey
-
Sebastian Berg
-
Stefano Borini
-
Stephan Hoyer
-
Steven D'Aprano
-
Todd