arif
arif

Reputation: 97

error when executing same loop

I'm new to Python and I really want to understand why I get this error.

It happens in my findLargest function, while trying to execute the second for loop. The thing is that the second for loop does basically the same thing as the first one, but for some reason I get an error as I try to call on a (class)method. How can this be? Am I not allowed to have 2 for loops for same iterable in the same function?

shapeArea=shape.area()   

throws:

TypeError: 'float' object is not callable

The objective of findlargest() is to loop through the set of classes twice, first in order to find the largest value(Area) while the second tries to find if there are other values that are equal.

class Shape(object):

    def area(self):
        raise AttributeException("Subclasses should override this method.")


class Triangle(Shape):
    def __init__(self, base, height):
        self.base = base
        self.height = height
    def area(self):
        self.area = (self.base * self.height) / 2
        return self.area
    def __str__(self):
        return "{} with base {} and height {}".format(self.__class__.__name__, self.base, self.height)
    def __eq__(self, other):
        return type(other) == Triangle and self.base == other.base and self.height == other.height

class ShapeSet:

    def __init__(self):
        self.shape_list = []

    def addShape(self, sh):

        if sh not in self.shape_list:
            self.shape_list.append(sh)

        else:
            print ("{} is already existing".format(sh.__str__())) 

    def __iter__(self):
        return (self.shape_list)     

    def __str__(self):
        s = ''
        for shape in self.__iter__():
            s+= shape.__str__() + "\n"
        return s

ss = ShapeSet()
ss.addShape(Triangle(1.2,2.5))
ss.addShape(Triangle(1.4,2.5))
ss.addShape(Triangle(1.3,2.5))
ss.addShape(Triangle(1.5,2.5))

def findLargest(shapes):
    maxs = None
    maxA = 0.0
    for shape in shapes.__iter__():
        shapeArea = shape.area()
        if shapeArea > maxA or maxs == None:
            maxs = shape
            maxA = shapeArea
    maxTuple = (maxs)
    for shape in shapes.__iter__():
        shapeArea = shape.area() 

Upvotes: 0

Views: 54

Answers (2)

TigerhawkT3
TigerhawkT3

Reputation: 49330

With this:

def area(self):
    self.area=(self.base*self.height)/2
    return self.area

You enter the method and then immediately mask it by assigning a different name to its reference. From then on, self.area refers to that number and you can no longer access that method. Fortunately, the fix is easy: don't save a reference at all.

def area(self):
    return self.base * self.height / 2

Python does not separate the names for function/method objects and for other objects. Use unique reference names for any objects you'd like to retain.

Upvotes: 1

gilhad
gilhad

Reputation: 609

I think your problem is here:

class Triangle(Shape):
def __init__(self, base, height):
    self.base=base
    self.height=height
def area(self):
    self.area=(self.base*self.height)/2 ###################### HERE
    return self.area

If you have

shape = Triangle(1.5,2.5)
shape.area() # returns float assigned at "HERE" to shape.area
shape.area() # try to call that float assigned in previous step at "HERE"

Upvotes: 0

Related Questions