Python Forum
TypeError: method missing 1 positional argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: method missing 1 positional argument
#1
Hi, I'm new to programming and have to write a program using methods and classes. I wrote this program and the logic makes sense to me but python throw me a TypeError. When I put an int in the get_name method it runs but when I don't it doesn't and I don't understand why. Can someone help me diagnose this? Here is my code:
class Pet: 

    def  __init__(self, name, animal_type, age):

        self.__name = name

        self.__animal_type = animal_type

        self.__age = age

    def set_name(self, name):

        self.__name = name

    def set_animal_type(self, animal_type):

        self.__animal_type = animal_type

    def set_age(self, age):

        self.__age = age

    def get_name(self, name):

        return self.__name

    def get_animal_type(self, animal_type):

        return self.__animal_type

    def get_age(self, age):

        return self.__age

def main():

    

    dog = Pet('1', '2', '3')

    print (dog.get_name())

main()
When I put any integer it 'here',

print (dog.get_name('here'))

it runs how I want it to. Why does it need an integer? Wall Wall
Reply
#2
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

Look at your definition of Pet, specifically the get_name method. You defined two parameters: self and name. The self parameter is provided automatically for instances of the class, but you have to provide name. If you don't, the function fails. You don't use the name parameter, so you should probably just remove it. You're going to have the same problem with your other getters.

BTW, this is Python, not Java. You never almost never use getters and setters in Python. They slow things down, make your code harder to understand, and don't actually do anything. If you really need that sort of security (as in, self.y must change whenever self.x changes) you use properties. But generally you just don't bother.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you. I should have read the rules before posting and will from now on. Thanks for the leniency and quick response.
Reply
#4
Now that I have a better grasp on what I was coding earlier in this program, I know why my assignment made me write setters and getters. I had to write the code this way to use accessors and mutators for the class.
Reply
#5
If that is what your teacher is requiring you to do, do it so that you can pass the class. Just be aware that it's really the wrong way to write Python.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __init__() missing 1 required positional argument: 'successor siki 1 4,289 Mar-08-2021, 02:05 PM
Last Post: Larz60+
  Missing 1 required positional argument in python code edwinostby 7 9,868 Jan-19-2021, 12:52 PM
Last Post: Serafim
  Missing positional arguments error?? hhydration 2 2,127 Oct-01-2020, 05:33 AM
Last Post: buran
  TypeError: Missing required positional arguments liaisa 7 28,849 Sep-25-2020, 08:16 PM
Last Post: deanhystad
  missing positional argument error programmert 1 2,803 Oct-18-2019, 11:05 AM
Last Post: Larz60+
  missing 1 required positional argument jedmond2 4 6,644 Sep-19-2019, 12:00 PM
Last Post: jefsummers
  missing 1 required positional argument mcgrim 10 19,721 May-07-2019, 09:02 PM
Last Post: Yoriz
  TypeError: __init__() missing 3 required positional arguments Pythonhelp82 6 23,066 Jan-24-2019, 04:25 AM
Last Post: Pythonhelp82
  another positional argument error (...and executing objects stored in a list) itmustbebunnies 7 4,181 Nov-16-2018, 07:18 PM
Last Post: itmustbebunnies
  Class positional argument error itmustbebunnies 2 2,985 Nov-07-2018, 11:09 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020