-1

This is my first coding class (and first time on the site) and I'm working on a class project. I can't seem to get my code to run correctly. I'm getting 2 errors and I've spent the last 2 hours trying to figure them out. Was wondering if this site would be willing/able to help me out. Code in reply message.

Traceback (most recent call last):
  File "C:\Users\Art\PycharmProjects\pythonProject1\main.py", line 69, in <module>
    class Conversion:
  File "C:\Users\Art\PycharmProjects\pythonProject1\main.py", line 123, in Conversion
    main()
TypeError: main() missing 1 required positional argument: 'self'
class Conversion:
    def say_hello(self):
        print("Hello! I'm a Conversion:", self)

    def main(self):
        task = Conversion()
        task.say_hello()

    if __name__ == "__main__":
        main()

Note: The original code has been removed, but feel free to look at it in the edit history. This is an MRE provided by wjandrea.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 3
    Why did you declare `main` as a member function of your class? You'd usually want that to be a free function – UnholySheep Oct 02 '22 at 22:25
  • The comments are mainly for other people to ask you for details if some part of your question isn't clear to them. If you have anything to add to your question, you should edit the question - partly because comments are limited in size and don't support formatting. As it is, I can't really understand what you're asking. – John Bayko Oct 02 '22 at 22:25
  • As @UnholySheep said, you probably should declare that separate from your class. That being said, `main()` as written will be implicitly given the argument `self` when you call `Conversion.main()` on an instance of `Conversion`. You don't have an instance of `Conversion`, so there's no `self` to pass. – Layne Bernardo Oct 02 '22 at 22:30
  • This should be a duplicate of [TypeError: Missing 1 required positional argument: "self"](https://stackoverflow.com/questions/17534345/) but I am out of close votes today. It is fundamentally the same problem with the same cause and solution. – Karl Knechtel Oct 02 '22 at 22:33
  • @Karl How is it a duplicate? Here the problem is that `main` is a method when it should be a function, and there the problem is that the method is being called on the class instead of an instance. – wjandrea Oct 02 '22 at 22:37
  • The problem is that *the class is not instantiated*. Making `main` a function doesn't solve that; it just kicks the can down the road (and in fact the error message will be the same, because the code calling `main()` runs immediately either way). The underlying question is "how do I use the methods of a class?" and the answer is "create an instance first, then call methods on the instance". The two questions just show two different wrong attempts at it. (Regardless, the question as asked doesn't meet standards for a variety of reasons.) – Karl Knechtel Oct 02 '22 at 22:41
  • @Karl Why do you think `main` is supposed to be a method? OP's a beginner, and it looks like they just indented the last section wrong, then got confused and added a `self` parameter for some reason. – wjandrea Oct 02 '22 at 22:44
  • I tried removing the main class and it no longer throws errors, but pycharm insisted in inserting def main(): pass just before "class conversion:" The initial main(self): was also inserted at the insistence of pycharm, so I'm really not sure why I need to declare it. – Not A Coder Oct 02 '22 at 22:49
  • 1
    Oh, I see now. `main` is intended to create the instance in question. Yes, that isn't a duplicate, then; but there is still a pretty simple typo underlying this. I don't see how a question can be extracted that can be stated clearly and be of use of others. Alternately, we can see it as three logical errors: not putting the `if __name__ == '__main__'` guard at top level (which would be a duplicate of one of the questions on that topic); not putting the corresponding `main` function as a function (which is a typo); and mistakenly giving it a parameter (which is a failure to attempt debugging). – Karl Knechtel Oct 02 '22 at 23:00
  • Removing the code from your question is considered vandalism (!), so I've undone it. If you want to remove your code from the site, you could delete your question entirely, but that could limit your ability to post here. Instead, you could remove the "meat" from your code and make a [mre], although keep in mind the complete code is still visible in the revision history, if that matters. BTW, I should have started with, welcome to Stack Overflow! Please take the [tour] and see [ask] for tips. You might also want to read [How to ask homework questions](//meta.stackoverflow.com/q/334822/4518341). – wjandrea Oct 02 '22 at 23:08
  • [Here's an MRE](https://gist.github.com/wjandrea/a09dcba8517caf07d2b4f3c9daa0e887) if you want to use it – wjandrea Oct 02 '22 at 23:20

2 Answers2

1

It looks like you indented the last section wrong and added a self parameter to main for some reason (edit: apparently your IDE did that). Your code should look like this:

class Conversion:
    ...
    
def main():
    ...
   
if __name__ == "__main__":
    main()  # on a separate line only for readability
wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

Looks like you're thinking like Java, where all functions like main() must be part of a class, but Python doesn't have that restriction, and plain functions are the norm.

In this case, you have code trying to call main(), but the function is defined within the class, so it would be called as self.main() - if that was what you should be doing, which in this case is not.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
John Bayko
  • 746
  • 4
  • 7
  • Your last paragraph is more confusing than helpful IMO. `self.main()` wouldn't work anyway since there is no `self` in the class body. – wjandrea Oct 02 '22 at 22:32