Error - __init__() takes exactly 2 arguments (1 given) - python

I'm trying to initialize the class (extraropt) from another .py but it gives me an error, I've searched but I haven't found a solution.
Heres the code of the one py I'm calling from:
main.py:
class GameWindow(ui.ScriptWindow):
def __init__(self, stream):
import extraop
exec 'extraop.extraropt().Show(stream)'
And here's the code of the one py I'm trying to call(init and del only):
extraop.py
class extraropt(ui.Window):
def __init__(self, stream):
ui.Window.__init__(self)
self.BuildWindow()
self.stream=stream
def __del__(self):
ui.Window.__del__(self)
It gives this error:
Error - __init__() takes exactly 2 arguments (1 given)

In the line
exec 'extraop.extraropt().Show(stream)'
You are implicitly calling extraropt.__init__() by creating a new instance of extraopt. In your code, you show that extraropt.__init__() takes a second (stream) argument, so you have to pass that in.
extraop.extraropt(stream).Show()
Incidentally, there is no reason why you should be doing an exec rather than explicitly calling it as I did above. There is also no reason for you to have a __del__() method defined as you only call the parent __del__() method anyway.

You need to initialize the parent this way:
super(extraropt, self).__init__(stream)

The stream variable in the line exec 'extraop.extraropt().Show(stream)' should be passed into the constructor of the extraropt class, like this:
exec 'extraop.extraropt(stream).Show()'

Related

__init__() missing 1 required positional argument it reads self as a parameter

I'm new at programming and I'm learning Python. The code should be very simple. The goal should be implement a calculator that does additions between numbers.
It returns this error:
init() missing 1 required positional argument: 'number_2'
So it's like it reads self as a parameter, but I can't figure out why.
I'm using Linux Ubuntu 19 as operative system.
Here's my code:
class Calculator:
def __init__(self, number_1, number_2):
self.number_1=number_1
self.number_2=number_2
def add(self):
print(f"{number_1}+{number_2}={number_1+number_2}")
if __name__=="__main__":
c=Calculator('Casio')
c.add(2,3)
It isn't reading self as a parameter here, but 'Casio' which it is storing as number_1. As the error message reads, it is missing number 2. If you want add() to be able to take arbitrary values, you will need to add them as arguments to that method rather than to the __init__() function.
You have to pass parameters to the add function and not to __init__ which instantiates the class.
class Calculator:
def __init__(self, name):
self.name=name
def add(self, number_1, number_2):
print(f"{number_1}+{number_2}={number_1+number_2}")
if __name__=="__main__":
c=Calculator('Casio')
c.add(2,3)
When you are initializing the object 'c', you are running the init method, and you therefore need to pass in both parameters to the init method. So, you are required to give both 'number_1' and 'number_2' when you create the object. You passed in only'Casio', which python is interpreting as 'number_1'. Python is also interpreting that there is no 'number_2'. Here are some solutions:
1: You could make the add() function have the two arguments that init has ('number_1' and 'number_2') and have the only arguments for init be self and 'name'. Then, you could have the init method only do self.name = name and nothing else. 2: You could make the arguments 'number_1' and 'number_2' optional by making a default variable for them if you do not enter it:
def __init__(self, number_1="1", number_2="2"):

Exception has occurred: TypeError start() takes 1 positional argument but 2 were given in multithreading python

i created the class webviewThread in which i have created the run function in which i am passing 2 arguments "self, openWhat" but it gives error on runtime. here is my code
class webviewThread(Thread):
def run(self,openWhat):
if openWhat=="facebook":
webview.create_window('Facebook', 'http://www.fb.com')
webview.start()
elif openWhat=="youtube":
webview.create_window('Facebook', 'http://www.youtube.com')
webview.start()
webObj=webviewThread()
def openfacebook():
webObj.start("facebook")
i am passing the value of argument but it gives error
In this line:
webObj=webviewThread()
You are creating a new instance of the webviewThread class. When you call methods on that instance (like start()) the self argument (which references the instance itself) will automatically be passed to the method as its first argument.
However, if you call a method on the class object itself, as you are doing here:
webviewThread.start()
self will not be passed, as your error indicates. Instead, you need to call the method on the instance of webviewThread that you created:
webObj.start()
From your code example though I don't see that the webviewThread class has a start() method, so you may want to call webObj.run()
Here are some resources that explain Python classes and self.
Self in Python class
The self variable in python explained
Python classes
Update:
With regards to the error listed in your updated post title: TypeError start() takes 1 positional argument but 2 were given, this is occurring because you are calling start() on your instance, and passing the "facebook" argument. Since you have not overridden the start() method of the superclass Thread, you will be calling Thread's start() method, which only takes a single argument: self (you are passing self, which happens automatically when you call the method on an instance, and "facebook" which you are passing explicitly).

Is it reasonable to let __init__ member must take self parameter?

I find when __init__ method lacks self parameter, only if there is no instantiation of this class, the compiler won't complain:
$ cat test.py
#!/usr/bin/python
class A():
def __init__():
print("A()")
$ ./test.py
But if there is a instantiation, the running time error will occur:
$ cat test.py
#!/usr/bin/python
class A():
def __init__():
print("A()")
A()
$ ./test.py
Traceback (most recent call last):
File "./test.py", line 6, in <module>
A()
TypeError: __init__() takes 0 positional arguments but 1 was given
Per my understanding, __init__() seems no use because it can't make any instance. So why doesn't the Python compiler enforce limitation such as when no self parameter in __init__ function, it will show error message? Is it reasonable? Or I miss something.
__init__ is not a special name at compilation time.
It is possible to do things with the symbol A.__init__ other than invoke it by trying to construct an A, and I think it is possible to replace the unusable __init__ function with a different function at runtime prior to trying to construct an A.
The check for a correct number of arguments is made when __init__ is called, as it is for any other method.
it must be one argument at least, we call it "self" usually, so your Python code maybe have a small change
#!/usr/bin/python
class A(object):
def __init__(self):
print("A()")
A()
__init__ is the initialization of an already created instance of a class, therefore it needs self, just like every other class method. If you want to handle the creation of the class, that is the __new__ method. This seems like a good description.
Don't let the fact that python doesn't complain unless the method is called confuse you.

Calling class methods in another python program - Unbound error issue

I have a program1.py that has the following structure:
program1.py
class program1_class()
def __init(self,var1):
command1
def method2(self,var1):
Then I have a program2 that imports the the class and needs to access the method2
program2.py
from program1_class import program1_class()
def method2(var1):
call_method2 = program1_class.method2(var1)
When I do this, I get the error
TypeError: unbound method predict_prob() must be called with LogisticRegressionSGD instance as first argument (got list instance instead)
There are a couple problems here-
from program1_class import program1_class() is invalid syntax in several ways.
The correct import statement is
from program1 import program1_class
Second, you tried to call a bound instance method on a class.
call_method2 = program1_class.method2(var1)
If you look at program1 you'll see that method2 is defined on the instance. You can tell this because the first argument is self. You call this by intantiating an instance of the class and then calling the method.
call_method2 = program1_class().method2(var1)

TypeError: functionname() takes exactly 2 arguments (1 given)

I'm trying to pass a dictionary that was initialized in one class as an argument into a method in another class. I wrote this code but no matter how I try to fix it I can't get going to test my other code..
I was trying to pass myDict as an argument in functionname by having class B inherit class A (I'm going to have other classes that will inherit class A and will have a function that takes myDict as an argument as well).
I have this:
class A(object):
def __init__(self):
class B(A):
.....
def functionname(self, myDict):
.....
def otherfunction(text):
text = function(otherargument) #this function returns a list and for each item in the list I call functionname.
myDict = {}
for x in text:
x.functionname(myDict)
I thought this would work but when I try to test my other code I get: TypeError: functionname() takes exactly 2 arguments (1 given). Am I not approaching this the right way?
This is homework so I would really appreciate some guidance on understanding the concept of this.
Edit: I called functionname from another function outside of the classes.
The error also starts at the function call (x.functionname()) and says TypeError
Edit: So I tried moving the dictionary that I declared in init to otherfunction and it worked and gave me different errors for other parts of code that needs to be fixed. But for this error, it was solved.

Categories

Resources