Python Super :Typeerror: _Init_() Takes 2 Positional Arguments But 3 Were Given

python super :TypeError: __init__() takes 2 positional arguments but 3 were given

You don't need to pass self here:

super(AndGate,self).__init__(self,n)

It should be

super(AndGate,self).__init__(n)

TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given while working with GUI

Turns out I was just way too tired when I got this error, I completely forgot to set up one of the other classes, which messed up the for loop. So lesson learned: don't try to fix stuff when you are tired.

Tkinter | TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given

To fix the error, you need to use the text keyword for the second argument of Checkbutton. All the options for Checkbutton need to be listed as key=value after the parent window argument.

import tkinter as tk
from tkinter import ttk

root = tk.Tk() # my main window

def cam1_name_func():
cam1_name.get()
amendations["camOne"] = cam1_name

cam1_name = tk.StringVar()
cam1 = ttk.Checkbutton(root, text=cam1_name).pack()

root.mainloop()

multiple inheritance, TypeError: __init__() takes 2 positional arguments but 3 were given

Make the base clases more flexible by having them pass extra *args on to the super call:

class A(): 
def __init__(self, a, *args):
super().__init__(*args)
self.a = a


class B():
def __init__(self, b, *args):
super().__init__(*args)
self.b = b


class C(A, B):
def __init__(self, a, b, c):
super().__init__(a, b)
self.c = c

>>> c = C(1,2,3)
>>> c.a
1
>>> c.b
2
>>> c.c
3

It would be even better to use keyword arguments in order not to rely on the mro:

class A(): 
def __init__(self, a=None, **kwargs):
super().__init__(**kwargs)
self.a = a


class B():
def __init__(self, b=None, **kwargs):
super().__init__(**kwargs)
self.b = b


class C(A, B):
def __init__(self, a, b, c):
super().__init__(a=a, b=b)
self.c = c

Now, class C(B, A) will work just as expected.

TypeError: __init__() takes 2 positional arguments but 3 were given - History and Settings classes should be well implemented?

The problem lies in the constructor for your Settings class, specifically:

super().__init__(self, parent)

When you call a method of an object, the object is implicitly added as the first parameter to the call. That means you are effectively passing two copies of self and one of parent, resulting in the error "I wanted two but you gave me three, so go away" (paraphrased).

In other words, do not explicitly pass self (match what you do in your History class):

super().__init__(parent)

Python: __init__() takes 2 positional arguments but 3 were given

I think this is the line that is causing the issue, you cannot pass the self instance to the constructor.

frame = F(self, container)

Can you please check and add more information to the question to understand what you are trying to achieve.

how to make subclass :-TypeError: __init__() takes 1 positional argument but 2 were given

I have written a working example for you. The code has many comments as explanation and for better understanding.

Code:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def sup(self):
# Change the "print" to "return" because the print is on caller side.
return f"{self.name} {self.age} {self.language}"


# The "Subclass" is inherited from "Person"
class Subclass(Person):
def __init__(self, name, age, language):
# Call the __init__ method of parent class with the proper parameters
super().__init__(name, age)
# Set the "language" as instance variable.
self.language = language


# The input parameters are separated so "," is needed instead of "+"
sm = Subclass(input("input your name: "), input("input your age: "), input("input your language: "))
print(sm.sup())

Output:

>>> python3 test.py 
input your name: Bill
input your age: 53
input your language: English
Bill 53 English

Notes:

  • I recommend to check the inheritation in Python: https://www.w3schools.com/python/python_inheritance.asp

  • The super() is Python3 specific: https://realpython.com/python-super/



Related Topics



Leave a reply



Submit