argparse modify

Mats Wichmann mats at wichmann.us
Thu Jun 23 19:01:42 EDT 2022


On 6/23/22 16:32, Dennis Lee Bieber wrote:
> On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer" <dieter at handshake.de>
> declaimed the following:
> 
>> ??? ???? wrote at 2022-6-23 15:31 +0300:
>>> how to solve this (argparse)
> 
>>>    MAXREPEAT = _NamedIntConstant(32,name=str(32))
>>> TypeError: 'name' is an invalid keyword argument for int()
>>
>> This does not look like an `argparse` problem:
>> the traceback comes from `oracle/RTR.py`.
> 
> 	And the listed code looks quite suspicious to me...
> 
>>>     class _NamedIntConstant(int):
>>>         def __init__(cls, value):
>>>             self = super(_NamedIntConstant, cls).__init__(cls, value)
>>>             self.name = name
>>>             return self
> 
> 	There does not appear to be any provision for keyword arguments at all.
> The only use of "name" is to an undefined object.

Indeed... a more typical version of this might be something like:

class _NamedIntConstant(int):
    def __init__(self, name, **kwargs):
        self.name = name
        super().__init__(**kwargs)

Assuming: that the "value" in your init method signature was supposed to
be 'name' since that's what you use later - and would explain your
exception!

In Python init methods are initializers, not creators (despite that many
materials call them constructors) - when you get to it, "self" already
exists, so you don't want to assign to it. And you don't return it -
again, it already exists, all you're doing here is setting it up, and
part of that you delegated to the parent class's initializer.

Also note that while it's claimed to be fine These Days, inheriting from
a base type like this is sometimes tricky, sometimes broken... be
somewhat aware.



More information about the Python-list mailing list