0

I'm still studying polymorphism in python. I tried to add color attribute based on this code, but I failed. Here is my code:

class Shape:
    width = 0
    height = 0
    color = 0
 
    def area(self):
        print('Parent class Area ... ')
    
    def get_color(self):
        print('Parent class Color ...')
 
 
class Rectangle(Shape):
 
    def __init__(self, w, h, c):
        self.width = w
        self.height = h
        self.color = c
 
    def area(self):
        print('Area of the Rectangle is : ', self.width*self.height)
    
    def get_color(self):
        print('Color of Rectangle: ', self.color)
 

class Triangle(Shape):
 
    def __init__(self, w, h, c):
        self.width = w
        self.height = h
        self.color = c
 
    def area(self):
        print('Color of Rectangle: ', self.color)
        print('Area of the Triangle is : ', (self.width*self.height)/2)

    def color(self):
        print('Color of Triangle: ', self.color)

Result:

TypeError: 'str' object is not callable

I'm still newbie in this part. Thank you for your helps before ;)

2
  • you are not showing which statement raises the TypeErros.. but I suspect this: your classes cannot have variables and methods with the same name, for example `color´ Commented Dec 9, 2021 at 10:07
  • Yes, I just realize that. Thank you :) Commented Dec 9, 2021 at 13:03

1 Answer 1

2

In your Triangle class, you have a method named color and also a property named color.
Change the color method to get_color it will resolve your problem.

 def get_color(self):
        print('Color of Triangle: ', self.color)

The python thinks you are calling the property named color which is a string and it cannot be called.

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

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.