0

I have a python3 class with the following code

class Cow:

    def __init__(self, *bovines):
        for i in bovines:
            self.i = i
            print(i)

    def moo(a,b):
        print(a)

animal = Cow
animal.moo('a','b')

It prints, correctly, a.

However, if I run the following (the only difference being that animal = Cow('Annie') instead of animal = Cow)

class Cow:

    def __init__(self, *bovines):
        for i in bovines:
            self.i = i
            print(i)

    def moo(a,b):
        print(a)

animal = Cow('Annie')
animal.moo('a','b')

Then moo returns the error

TypeError: moo() takes 2 positional arguments but 3 were given

I imagine this has something to do with the function accepting whatever is __init__'d as an argument, but I'm not sure how to work around this.

Thanks for any help!

Michael
  • 5
  • 3
  • "I imagine this has something to do with the function accepting whatever is `__init__`'d as an argument" Notice how the code you posted does **not** contain a method named `__init__`? It has been named `__init` instead. This is, presumably, the cause of the problem - although the usage example you posted doesn't make any sense: "instantiate `animal` with `cow`" already doesn't make sense, and you show the same code block again without indicating what is done differently to cause the problem. – Karl Knechtel Jul 07 '21 at 01:02
  • 1
    In the future, please don't rely on vague descriptions such as "with a structure something like this" - instead, create a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Karl Knechtel Jul 07 '21 at 01:03
  • Thank you. This was an error in the formatting. The code blocks aren't identical now. I think this is minimal and reproducible now. I also fixed capitalization. – Michael Jul 07 '21 at 01:04
  • I'm fixing those now. – Michael Jul 07 '21 at 01:10
  • I think this is minimal and reproducible now. – Michael Jul 07 '21 at 01:18
  • Yay it is neatly reproducible! – j1-lee Jul 07 '21 at 01:28
  • Ah, now I understand. Please see https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given . – Karl Knechtel Jul 07 '21 at 01:46
  • 1
    Your "only" difference is a *huge* difference. `animal = Cow` doesn't create an instance of `Cow` at all; it just makes `animal` another reference to the class. – chepner Jul 07 '21 at 02:11

1 Answers1

0

When defining a method for a class in python, you need to have self as the first parameter passed into the definition. See below:

def moo(self, a, b):
    print(a)

The type error is because you are passing in self and then the parameters you'd like to pass in (a & b) to the function moo by calling it. Hence "TypeError: moo() takes 2 positional arguments but 3 were given".

If you were defining a function but not passing in any parameters, you'd still define it as:

def foo(self):
  # do stuff
Michael
  • 5
  • 3
zahlawi
  • 97
  • 8