Friday , September 20 2024

If the class constructor is declared in the following way: Which one of the assignments is invalid? class Class: def __init__(self, val=0): pass

Questions BankCategory: Python Essentials 2If the class constructor is declared in the following way: Which one of the assignments is invalid? class Class: def __init__(self, val=0): pass
If the class constructor is declared in the following way:
Which one of the assignments is invalid?

class Class:
    def __init__(self, val=0):
        pass
  • object = Class(1)
  • object = Class()
  • object = Class(None)
  • object = Class(1, 2)

Explanation: An attempt to create an object of class Class using the assignment object = Class(1, 2) will raise a TypeError exception, because a maximum of two arguments are accepted by the __init__ declaration, not three arguments (self, 1, and 2).

More Questions: Python Essentials 2 (PE2) Course Final Test