3 ms·
There are bugs in this code and I'm glad that I'm not the only one that has done it! graph = { key: { p for p in inspect.si
by VWWHFSfQ 5y ago
There are bugs in this code and I'm glad that I'm not the only one that has done it!
graph = {
key: {
p
for p in inspect.signature(method).parameters.keys()
if p != "self" and not p.startswith("_")
}
for key, method in cls._registry.items()
}
The first parameter to a bound method does not have to be called `self`. It's conventional, but not required. Is there a better way in the inspect module to filter these parameters? This comes up more often with classmethods where the naming convention `cls` is most common but I see `klass` somewhat frequently as well.
- deleted 5y ago[deleted]
- GVRV 5y agoThis got me thinking and I'm not even seeing the `self` being returned in the list of parameters on Python 3.8.5: >>> class Test: ... def test(self, a, b, c=5): ... return a + b + c ... >>> t = Test() >>> inspect.signature(t.test).parameters mappingproxy(OrderedDict([('a', <Parameter "a">), ('b', <Parameter "b">), ('c', <Parameter "c=5">)]))
- Starlevel001 5y agoThat's because ``t.test`` returns a MethodType instance rather than a FunctionType instance, which is invoked without the self.
- GVRV 5y agoAh, you're absolutely correct! In [4]: class Test: ...: def test(self, a, b, c=5): ...: return a + b + c ...: In [5]: inspect.signature(Test.test).parameters Out[5]: mappingproxy({'self': <Parameter "self">, 'a': <Parameter "a">, 'b': <Parameter "b">, 'c': <Parameter "c=5">}) Then, any idea how you would address the GP's original point? How should "self" be detected if it can be called something else?