Home > OS >  Property method in python not returning correct value
Property method in python not returning correct value

Time:12-17

I am trying to solve the question in which I am asked to use property method to count the number of times the circles are created . Below is the code for the same.

import os
import sys

#Add Circle class implementation below
class Circle:
    counter = 0
    def __init__(self,radius):
        self.radius = radius
        Circle.counter = Circle.counter   1
        
    def area(self):
        return self.radius*self.radius*3.14
    
    def counters():
        print(Circle.counter)
    no_of_circles = property(counter)
      


if __name__ == "__main__":
    
        res_lst = list()
        lst = list(map(lambda x: float(x.strip()), input().split(',')))
        
        for radius in lst:
            res_lst.append(Circle(radius).area())
        
        print(str(res_lst), str(Circle.no_of_circles))

The above code gives correct output for the area but counter should be 3 and instead I am getting below output . Below is the output for input = 1,2,3

[3.14, 12.56, 28.26] <property object at 0x0000024AB3234D60>

I have tried everything but no luck. In the main section of the code no_of_circles is called as Circle.no_of_circles which suggests me that it will use property method of python. But the output is wrong. Please help me find where I am going wrong.

CodePudding user response:

str(Circle.no_of_circles) here you're calling the property of a class not an instance of that class i.e a Circle object; the following will work :-

class Circle:
    _counter = 0
    def __init__(self,radius):
        self.radius = radius
        Circle._counter  = 1

    def area(self):
        return self.radius*self.radius*3.14

    @property
    def no_of_circles(self):
        return self._counter

if __name__ == "__main__":
        lst = list(map(lambda x: float(x.strip()), input("Enter radius : ").split(',')))
        cir_lst = [Circle(_) for _ in lst]
        res_lst = [__.area() for __ in cir_lst]

        print(res_lst, cir_lst[-1].no_of_circles)

CodePudding user response:

Here is a simple working example using the property function. Note: It is always a good practice to make an instance of a class(once) and then uses the instance all over your code. Also better to use self.counter instead of Cirlcle.counter. self is Circle.

#Add Circle class implementation below
class Circle:
    
    def __init__(self,value=0):
        self._counter = value
        
    def area(self, radius):
        return radius*radius*3.14
    
    def add_counter(self, value):
        print('add counter')
        self._counter  = 1

    def get_counter(self):
        print('get counter')
        return self._counter

    no_of_circles = property(get_counter, add_counter)
      


if __name__ == "__main__":
    circle = Circle()
    area = []
    for idx in range(5):
        area.append(circle.area(idx))
        circle.add_counter(1)
    print("number of calls: ", circle.no_of_circles)
    print('area:', area)

output(note how get and add counter are called):

add counter
add counter
add counter
add counter
add counter
get counter
number of calls:  5
area: [0.0, 3.14, 12.56, 28.26, 50.24]
  • Related