1

I am new to python and trying to write a program with class and object. Following is my code:

class Animal:
    __name = ""
    __height = 0
    __weight = 0
    __sound = 0

# define a constructor and pass arguments
def __init__(self, name, height, weight, sound):
    self.__name = name
    self.__height = height
    self.__weight = weight
    self.__sound = sound


# set and get functions for name
def set_name(self, name):
    self.__name = name

def get_name(self):
    return self.__name


# set and get functions for height
def set_height(self, height):
    self.__height = height

def get_height(self):
    return str(self.__height)


# set and get functions for weight
def set_weight(self, weight):
    self.__weight = weight


def get_weight(self):
    return str(self.__weight)


# set and get functions for sound
def set_sound(self, sound):
    self.__sound = sound

def get_sound(self):
    return self.__sound


def get_type(self):
    print("Animal")


def toString(self):
    return "{} is {} cm tall and {} kg and say {}".format(self.__name, self.__height, self.__weight, self.__sound)


# create an object call cat of type Animal
cat = Animal('Whiskers', 33, 10, 'Meow')
print(cat.toString())

when I run the program it gives me an error that object takes no parameters. but I have described the parameters in the constructor of the class. Please Help.

5
  • 7
    Did you indent your code the way you posted it here? If so, then that is your problem. Commented Dec 12, 2016 at 17:47
  • 1
    ... because if so, then __init__() is not a method, but a simple function and Animal only inherits __init__() from object, hence the error message. Commented Dec 12, 2016 at 17:48
  • Try to start smaller. There's no reason to have more than the constructor for your error. Besides that, Python is not Java. Overriding toString in Java is the same as implementing __str__ in Python Commented Dec 12, 2016 at 17:50
  • can you please explain it in a bit details. I am new to python and trying to learn from mistakes..what do u mean by indent the code?? and what can I do to improve it? Commented Dec 12, 2016 at 17:50
  • 1
    Whitespace is very important. Read through the tutorial. docs.python.org/3.6/tutorial/… Commented Dec 12, 2016 at 17:52

2 Answers 2

5

Make sure all your functions for Animal are indented properly. They need to be inside of class Animal

Sign up to request clarification or add additional context in comments.

Comments

3

As mentioned by the others, the __init__ method needs to be indented inside of class Animal for it to be considered a constructor.

But, be careful after you do this. The way that __name, __height, __weight, and __sound are declared right now, they are static class variables. However, in __init__ (by prefixing self. in front of the variables) you would be re-declaring instance variables with the name variable names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.