TypeError: main() missing 1 required positional argument: self

2024/10/6 0:26:08

My code and error is below and I was trying to understand why I am getting the error and how to fix it. I tried this without self and got another error

TypeError: load_data() takes 0 positional arguments but 1 was given. 
def main(self):training_loader, validation_loader, testing_loader = Utilities3.load_data(data)model, optimizer, criterion = Utilities3.network_construct(structure, drop, hidden_layer, learningrate, device)Utilities3.do_deep_learning(model, optimizer, criterion, epochs, 40, training_loader, device)Utilities3.save_checkpoint(model, path, structure, hidden_layer, drop, learningrate)print("Training is finish")if __name__== "__main__":main()
TypeError                                 Traceback (most recent call last)
<ipython-input-25-77e46aea71ac> in <module>()49 50 if __name__== "__main__":
---> 51     main()TypeError: main() missing 1 required positional argument: 'self'
Answer

Your function main takes in the argument self, but in your execution, main(), you do not pass in any arguments.

First, self is used in object oriented programming, when you have a class with attributes and methods. But that is not what you have here. Moreover, you don't seem to be using self at all in the function, so why do you have it as an argument?

Do this instead:


def main():training_loader, validation_loader, testing_loader = Utilities3.load_data(data)model, optimizer, criterion = Utilities3.network_construct(structure, drop, hidden_layer, learningrate, device)Utilities3.do_deep_learning(model, optimizer, criterion, epochs, 40, training_loader, device)Utilities3.save_checkpoint(model, path, structure, hidden_layer, drop, learningrate)print("Training is finish")if __name__== "__main__":main()
https://en.xdnf.cn/q/119008.html

Related Q&A

Python not calling external program

I am having problems with a python program that I wrote. It is actually plpython3u. I am running the program as a Trigger from postgres. I am pretty sure the trigger part works. My test python prog…

Selenium: How do I retry browser/URL when ValueError(No tables found)

I have a code that scrapes oddsportal website. Sometimes while scraping, I get ValueError("No tables found") and when I manually refresh browser, page loads. How do I do it via code? My code…

For loop for web scraping in python

I have a small project working on web-scraping Google search with a list of keywords. I have built a nested For loop for scraping the search results. The problem is that a for loop for searching keywor…

operation on a variable inside a class in python

Im new with oop and python. Ive been trying to do a simple thing: there is class called Foo(),it contains a variable called x which is initially set to zero.>>>a = Foo() >>>a.x >&g…

Print several sentences with different colors

Im trying to print several sentences with different colors, but it wont work, I only got 2 colors, the normal blue and this redimport sys from colorama import init, AnsiToWin32stream = AnsiToWin32(sys.…

Discord bot to send a random image from the chosen file

I am making a discord bot that randomly chooses an image (images) which is in the same directory (Cats) as the python file(cats.py). This is what my code looks like right now: Cats = os.path.join(os.pa…

pytest - patched method of a class does not return the mock value

My code is fairly simple but i dont understand what is going on :class MyDb :def some_func( arg ) :....while my test code is :@mock.patch(mypkg.mydb) @pytest.mark.parametrize( func_dummy_value ) :( [ {…

New instance of toplevel classes make overlapping widgets

Im generally new to python and tkinter. Ive been programming maybe about a year or so, and Ive just started to try to make each tkinter toplevel window its own class because Ive heard that its the righ…

Regex End of Line and Specific Chracters

So Im writing a Python program that reads lines of serial data, and compares them to a dictionary of line codes to figure out which specific lines are being transmitted. I am attempting to use a Regul…

Is it possible to scrape webpage without using third-party libraries in python?

I am trying to understand how beautiful soup works in python. I used beautiful soup,lxml in my past but now trying to implement one script which can read data from given webpage without any third-party…