Why are you making a mistake in calling a method?

Asked

Viewed 95 times

-4

I’m trying this code:

class Car:
    def beep():
        print('Beep')

car = Car()
car.beep()

Typeerror: method() takes 0 positional Arguments but 1 was Given

'Cause there’s a mistake?

  • 2

    Strip the () of Car()

  • 3

    @Fourzerofive if it is python3 this "almost" right, but the author did not indicate whether it is 2 or 3, which is already a question problem ... if it is 2 will have the error: TypeError: unbound method beep() must be called with Car instance as first argument (got nothing instead), have to be sure if the case is in fact OO or other thing the author wants, remove () can solve apparently, but no use "solve" without the real technical need being evident, because in fact it may just be generating another problem.

  • 1

    Hello @Marconciliosouza, is a good link, but as I said, we need to make sure what the real need of the code, if it is really OO, if you need the method, if it is learning OO it is not much use just to arrive and demonstrate the "means" without having a clear context of your needs. The link is not bad, as I said, but it’s just a series of how to solve problems without understanding what they are for.

  • Hello @Marconciliosouza removed not, edited by the author and detailed. I don’t think the link is bad as I said before, it even explains some things, but the answer below also explains (remember that we are a community in Portuguese). But I’m just trying to steer "everyone" that the question is problematic, I just held on to close it because I saw activity of 3 users, apart from the author and I’m evaluating how to do this without causing harm to anyone. I hope you understand that I came here just wanting to help and of course hoping to see your opinions as well. Thank you.

  • The question isn’t in the best shape, but you don’t have much trouble answering that. I was curious to know what the wrong answer is, because I myself want to learn from the person who has negatived and knows something that I do not know about.

  • @Marconciliosouza What details do you want? It doesn’t matter what version of Python to solve this. The error has nothing to do with the first comment, which in fact deviated completely from the real subject of the question and then people started to think that the question is about something else, but it is the fault of the comments and not the question. If the answer isn’t wrong, it doesn’t deserve negative. And the answer solved the problem, the AP even came to comment, but I deleted it because it’s not relevant comment. You want more details, but the question is very simple, it has become an ivory tower. There is nothing to discuss.

  • @Marconciliosouza I saw an opportunity to explain something that many people confuse, unfortunately the rifle they made threw everything in the trash, and when I have other opportunity of this I will have to answer again. The site exists to create content that teaches people to solve their problems and the question was good for it, too bad they screwed up. If you had the opportunity to respond well, you don’t have to be closed. You created a confusing situation, the question wasn’t confusing, it just wasn’t wonderful.

  • People need to understand what the purpose of the site is and here it has been achieved (except for this lot of negatives on something that is not so bad, there is something much worse, something wrong, that misfortune, that causes problems that do not have so much. And this attitude tip ode has been discussed before in the goal, people go into a positive or negative spiral according to the flow and not according to the quality of the content. It does not deserve positive, but not that negative all. The answer then neither speaks, nor who gave agrees that it is wrong.

  • You can signal or discuss at the goal all elaborate questions that have been closed. Writing a lot of stuff doesn’t mean you can respond well, it’s usually what causes confusion. Some people accuse Sopt of not liking newbies, I like so much that simple questions are answered by me. I don’t like people with no idea, who take something complex to do and no conditions, want other people to solve for her. Who does not like beginner is who does not understand the site.

Show 4 more comments

1 answer

6

The method beep() has no parameter, and you are passing a argument when you call it. Placing the parameter self which is always mandatory when creating a method solves the problem:

class Car:
    def beep(self):
        print('Beep')

car = Car()
car.beep()

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

When you call car.beep() is actually calling a function that way: Car.beep(car), ie, is calling a function called beep() surname Car, since it is only seen within this class, and is passing the value of the variable car as a function argument. Then you have to have a variable as a parameter of that function, and you have agreed that being a method the name of that parameter is self.

That’s how it works too:

class Car:
    def beep():
        print('Beep')

car = Car()
Car.beep()

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But it is not what you want, it is not an instance method, it [and static.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.