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


Messages In This Thread
error occuring in definition a class - by akbarza - Nov-25-2023, 08:04 AM
RE: error occuring in definition a class - by Yoriz - Nov-25-2023, 02:01 PM
RE: error occuring in definition a class - by Yoriz - Nov-26-2023, 09:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 310 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  mutable argument in function definition akbarza 1 523 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 626 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 773 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  Explain the python code in this definition Led_Zeppelin 1 1,131 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  meaning of -> syntax in function definition DrakeSoft 5 2,034 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,138 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Something wrong with the quotation mark in dictionary definition Mark17 1 2,037 Jan-29-2021, 03:34 PM
Last Post: buran
  error in class non_name092 1 1,971 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  My class has name error message 357mag 3 2,374 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