Python Forum
error occuring in definition a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error occuring in definition a class
#1
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:
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
Reply
#2
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)
akbarza likes this post
Reply
#3
hi can explain what does the line
obj = super().__new__(cls)
do?
Reply
#4
https://docs.python.org/3/reference/data...ct.__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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 269 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  mutable argument in function definition akbarza 1 492 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 595 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 750 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  Explain the python code in this definition Led_Zeppelin 1 1,106 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  meaning of -> syntax in function definition DrakeSoft 5 1,977 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,945 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Something wrong with the quotation mark in dictionary definition Mark17 1 2,003 Jan-29-2021, 03:34 PM
Last Post: buran
  error in class non_name092 1 1,948 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  My class has name error message 357mag 3 2,345 Sep-04-2019, 03:29 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020