Nov-25-2023, 08:04 AM
hi
the below code is in :https://python.coderz.ir/lessons/l17-obj...ython.html and it has been ran correctly
but when I did run in idle I encountered an error:
thanks
the below code is in :https://python.coderz.ir/lessons/l17-obj...ython.html and it has been ran correctly
but when I did run in idle I encountered an error:
class Sample: def __new__(cls, *args, **kwargs): print("__new__(), Has been called") print('cls: ', cls) print('args: ', args) print('kwargs: ', kwargs) # create new object obj = super().__new__(cls, *args, **kwargs) # return object return obj def __init__(self, x=0, y=0): print("__init__(), Has been called") print('self: ', self) self.x = x self.y = yin idle i wrote:
sample_1 = Sample()the output was:
Output:__new__(), Has been called
cls: <class '__main__.Sample'>
args: ()
kwargs: {}
__init__(), Has been called
self: <__main__.Sample object at 0x000002425DED2450>
when i wrote: sample_2 = Sample(3, 6), the output was:
Output:__new__(), Has been called
cls: <class '__main__.Sample'>
args: (3, 6)
kwargs: {}
Error:Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
sample_2 = Sample(3, 6)
File "<pyshell#46>", line 10, in __new__
obj = super().__new__(cls, *args, **kwargs)
TypeError: object.__new__() takes exactly one argument (the type to instantiate)
and when i wrote: sample_3 = Sample(x=3, y=6)the output was:
Output:__new__(), Has been called
cls: <class '__main__.Sample'>
args: ()
kwargs: {'x': 3, 'y': 6}
Error:Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
sample_3 = Sample(x=3, y=6)
File "<pyshell#46>", line 10, in __new__
obj = super().__new__(cls, *args, **kwargs)
TypeError: object.__new__() takes exactly one argument (the type to instantiate)
what is the problem with the above?thanks