Re: TypeError: 'kwarg' is an invalid keyword argument for this function
On 13/10/2014 8:04 PM, Dave Angel wrote: It would also help to spell it the same. In the OP's implementation, he defined kwargs, and tried to use it as kwarg. That's perfectly okay, though: if `kwargs` is the name used to reference the dictionary of keyword arguments, `kwarg` would be an instance of a keyword argument. -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'kwarg' is an invalid keyword argument for this function
Ian Kelly ian.g.ke...@gmail.com Wrote in message: On Sun, Oct 12, 2014 at 6:55 AM, roro codeath rorocode...@gmail.com wrote: How to implement it in my class? class Str(str): def __init__(self, *args, **kwargs): pass Str('smth', kwarg='a') The error is coming from the __new__ method. Because str is an immutable type, you should override the __new__ method, not __init__. Example: class Str(str): def __new__(cls, *args, **kwargs): return super().__new__(cls, args[0]) Str('smth', kwarg='a') 'smth' It would also help to spell it the same. In the OP's implementation, he defined kwargs, and tried to use it as kwarg. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'kwarg' is an invalid keyword argument for this function
Dave Angel wrote: Ian Kelly ian.g.ke...@gmail.com Wrote in message: On Sun, Oct 12, 2014 at 6:55 AM, roro codeath rorocode...@gmail.com wrote: How to implement it in my class? class Str(str): def __init__(self, *args, **kwargs): pass Str('smth', kwarg='a') The error is coming from the __new__ method. Because str is an immutable type, you should override the __new__ method, not __init__. Example: class Str(str): def __new__(cls, *args, **kwargs): return super().__new__(cls, args[0]) Str('smth', kwarg='a') 'smth' It would also help to spell it the same. In the OP's implementation, he defined kwargs, and tried to use it as kwarg. That is consistent as should become clear when using something completely different: class Str(str): ... def __init__(self, *args, **kwargs): pass ... Str(smth, alpha=beta) Traceback (most recent call last): File stdin, line 1, in module TypeError: 'alpha' is an invalid keyword argument for this function -- https://mail.python.org/mailman/listinfo/python-list
TypeError: 'kwarg' is an invalid keyword argument for this function
How to implement it in my class? class Str(str): def __init__(self, *args, **kwargs): pass Str('smth', kwarg='a') # How to implement in my class class C: def __init__(self): pass class C2(C): def __init__(self, *args, **kwargs): pass C2(kwarg='a') -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'kwarg' is an invalid keyword argument for this function
Whats the problem?? Seems to work (python 2.7.8) [Ive added a line so that that you can see] class C: def __init__(self): pass class C2(C): def __init__(self, *args, **kwargs): self.dic = kwargs pass x = C2(kwarg='a') y = C2(kwarg='a', kwarg2=8) x.dic {'kwarg': 'a'} y.dic {'kwarg': 'a', 'kwarg2': 8} -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'kwarg' is an invalid keyword argument for this function
On Sun, Oct 12, 2014 at 6:55 AM, roro codeath rorocode...@gmail.com wrote: How to implement it in my class? class Str(str): def __init__(self, *args, **kwargs): pass Str('smth', kwarg='a') The error is coming from the __new__ method. Because str is an immutable type, you should override the __new__ method, not __init__. Example: class Str(str): def __new__(cls, *args, **kwargs): return super().__new__(cls, args[0]) Str('smth', kwarg='a') 'smth' -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'kwarg' is an invalid keyword argument for this function
On 10/12/2014 2:45 PM, Ian Kelly wrote: On Sun, Oct 12, 2014 at 6:55 AM, roro codeath rorocode...@gmail.com wrote: How to implement it in my class? class Str(str): def __init__(self, *args, **kwargs): pass Str('smth', kwarg='a') The error is coming from the __new__ method. Because str is an immutable type, you should override the __new__ method, not __init__. or, if one is mere adding a method, do not add __new__ either. Example: class Str(str): def __new__(cls, *args, **kwargs): return super().__new__(cls, args[0]) Str('smth', kwarg='a') 'smth' -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list