Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class takes no arguments
#1
Hey,

I am writing a basic class and I am having problems inserting arguments into the class "Dog".

Here is the source code

class Dog():
    def _init_(self, name, age):
        self.name = name
        self.age = age

    def sit(self):
        print(self.name.title() + ' is now sitting.')

    def roll_over(self):
        print(self.name.title() + ' is now rolling over.')


my_dog = Dog('Willie', 6)
print("my dog's name is " + my_dog.name.title() + ".")
print('my dog is ' + str(my_dog.age) + '.')
here is the error message:
Error:
Traceback (most recent call last): File "C:/Users/maxya/PycharmProjects/classes.py", line 28, in <module> my_dog = Dog('Willie', 6) TypeError: Dog() takes no arguments
Thank you for your help.
Reply
#2
The dunder init method is double underscore on each side __init__
Recommended Tutorials:
Reply
#3
Ok thank you so much!
Reply
#4
(Jul-10-2019, 11:18 PM)Myang123 Wrote: Ok thank you so much!

Hello Myang123,

I saw this same problem and one question I had was already answered, but I had another that I hope you can help with.

How did you get the line below to work?

my_dog = Dog("Peso")

I placed it outside the code block(>>>) and inside the code block(...) and both failed.

The return message is(...) invalid syntax and (>>>) Dog is not defined.

I am hoping that you are able to point me in the right direction.

Thanks!
DJXIV
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class takes no arguments bily071 2 597 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,857 Oct-17-2022, 06:29 PM
Last Post: paulo79
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Class Takes No Arguments horuscope42 4 4,752 Oct-26-2020, 11:10 PM
Last Post: not_username1234
  Class takes no arguments Nazartfya 2 4,503 Jun-27-2020, 12:45 PM
Last Post: Nazartfya
  Why Car() takes no arguments louis216 2 2,544 Jun-25-2020, 03:16 AM
Last Post: louis216
  random.choice() takes two positional arguments, but three were given. ShakeyPakey 5 11,477 May-31-2020, 03:13 PM
Last Post: deanhystad
  Question() takes no arguments jwrcfv 2 3,055 Apr-02-2020, 06:08 PM
Last Post: jwrcfv
  add() takes 2 positional arguments but 3 were given Man_from_India 3 5,698 Feb-10-2020, 05:08 PM
Last Post: Man_from_India
  This constructor takes no arguments Friend 2 5,268 Jun-26-2019, 02:54 PM
Last Post: Friend

Forum Jump:

User Panel Messages

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