Python Forum
[split] Class takes no arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Class takes no arguments
#1
Hi,
I am a beginner and learning python basics from book Python Crash Course but can't figure this out Wall :

class Dog():
     """A simple attempt to model a dog."""
     def __init__(self, name, age):
         """Initialize name and age attributes."""
         self.name = name
         self.age = age
 
     def sit(self):
         """Simulate a dog sitting in response to a command."""
         print(self.name.title() + " is now sitting.")

     def roll_over(self):
         """Simulate rolling over in response to a command."""
         print(self.name.title() + " rolled over!")

        
#Making an Instance from a Class
class Dog():
    my_dog = Dog('Atos', 8)
    print("My dog name is " + my_dog.name.title() + ".")
    print("My dog is " + str(my_dog.age) + " years old.")
    
#Accessing Attributes
#Calling Methods
class Dog():
    my_dog = Dog('Atos', 8)
    my_dog.sit()
    my_dog.roll_over()
and I am getting this output:

My dog name is Atos.
My dog is 8 years old.
Error:
Traceback (most recent call last): File "C:\Users\User\Desktop\exercise.py", line 25, in <module> class Dog(): File "C:\Users\User\Desktop\exercise.py", line 26, in Dog my_dog = Dog('Atos', 8) TypeError: Dog() takes no arguments
could someone pls help
buran write Oct-23-2023, 04:52 PM:
I split the post. I didn't realize you hi0jack thread
deanhystad write Oct-23-2023, 03:45 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Lines 18 and 25 and unnecessary. You already have defined the class

class Dog():
     """A simple attempt to model a dog."""
     def __init__(self, name, age):
         """Initialize name and age attributes."""
         self.name = name
         self.age = age
  
     def sit(self):
         """Simulate a dog sitting in response to a command."""
         print(f'{self.name.title()} is now sitting.')
 
     def roll_over(self):
         """Simulate rolling over in response to a command."""
         print(f'{self.name.title()} rolled over!')
 
         
#Making an Instance from a Class

my_dog = Dog('Atos', 8)
print(f"My dog name is {my_dog.name.title()}.")
print(f"My dog is {my_dog.age} years old.")
     
my_dog.sit()
my_dog.roll_over()
bily071 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
If you have a question, create a new post. Don't post your question as a reply to an existing post.

You create a class named Dog, then you create a different class and name it Dog. Finally you create yet a third class named Dog. If you want to create an instance of Dog and call the Dog methods, stop making new Dog classes.
#Making an Instance from a Class
my_dog = Dog('Atos', 8)
print("My dog name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")

my_dog.sit()
my_dog.roll_over()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 494 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,962 Oct-17-2022, 06:29 PM
Last Post: paulo79
  Split Arguments ? jesse68 3 1,039 Jun-24-2022, 07:32 PM
Last Post: Larz60+
  Checking the number of arguments a function takes Chirumer 3 2,173 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Class Takes No Arguments horuscope42 4 4,856 Oct-26-2020, 11:10 PM
Last Post: not_username1234
  Class takes no arguments Nazartfya 2 4,604 Jun-27-2020, 12:45 PM
Last Post: Nazartfya
  Why Car() takes no arguments louis216 2 2,630 Jun-25-2020, 03:16 AM
Last Post: louis216
  random.choice() takes two positional arguments, but three were given. ShakeyPakey 5 11,737 May-31-2020, 03:13 PM
Last Post: deanhystad
  [split] Python Class Problem astral_travel 12 5,027 Apr-29-2020, 07:13 PM
Last Post: michael1789
  Question() takes no arguments jwrcfv 2 3,113 Apr-02-2020, 06:08 PM
Last Post: jwrcfv

Forum Jump:

User Panel Messages

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