Python Forum
[split] Class takes no arguments - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [split] Class takes no arguments (/thread-40978.html)



[split] Class takes no arguments - bily071 - Oct-23-2023

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


RE: Class takes no arguments - buran - Oct-23-2023

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()



RE: Class takes no arguments - deanhystad - Oct-23-2023

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()