error occuring in definition a class - akbarza - Nov-25-2023
hi
the below code is in :https://python.coderz.ir/lessons/l17-object-oriented-programming-with-python.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 = y in 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
RE: error occuring in definition a class - Yoriz - Nov-25-2023
As per the error TypeError: object.__new__() takes exactly one argument (the type to instantiate)
Change
obj = super().__new__(cls, *args, **kwargs) to only pass cls
obj = super().__new__(cls)
RE: error occuring in definition a class - akbarza - Nov-26-2023
hi can explain what does the line obj = super().__new__(cls) do?
RE: error occuring in definition a class - Yoriz - Nov-26-2023
https://docs.python.org/3/reference/datamodel.html?highlight=__new__#object.__new__ Wrote:object.__new__(cls[, ...])
Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).
Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super().__new__(cls[, ...]) with appropriate arguments and then modifying the newly created instance as necessary before returning it.
If __new__() is invoked during object construction and it returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to the object constructor.
If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.
__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.
|