LDs
LDs

Reputation: 13

python bound method Error

I have a problem about python 2.7 and the def functions in a class as there're problems about bound method. And this is an assignment from school :D

Here's the code :

from abc import ABCMeta, abstractmethod import math

class Shapes(object):
    __metaclass__= ABCMeta
    @abstractmethod

    def __init__(self):
        pass

class TwoDShapes(Shapes):
    def __init__(self):
        pass

class ThreeDShapes(Shapes):
def __init__(self):
        pass
================= Main.py =====================
from BasicClassShapes import*

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        self.perimeter = length*2 + width*2
    def Area(self):
        self.Area = length*width

====================================

def PrintALL():

    A = Rectangle("Rectangle", "10", "20", "4",  "4")

    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter
    print "This is the area: ", A.Area
    print "\n"
PrintALL()


=============== Result =======================
This is the name of the shape:  Rectangle
This is the length:  10
This is the width:  20
This is the number of side:  4
This is the number of vertice:  4
This is the perimeter:  <bound method Rectangle.perimeter of <__main__.Rectangle object at 0x03BEFC90>>

This is the area:  `<bound method Rectangle.Area of <__main__.Rectangle object at 0x03BEFC90>>`

Upvotes: 1

Views: 10753

Answers (4)

Shahriar Sadat
Shahriar Sadat

Reputation: 21

class New:
    def __init__(self,a,b):
        self.a = a
        self.b = b


    def addition(self):
        return (self.a + self.b)

p = New(3,4)

print(p.a)
print(p.b)

print(p.addition())

Look at this code snippet, You need to call the function by using (), functions as p.addition() and variables as p.a

Upvotes: 0

LDs
LDs

Reputation: 13

from BasicClassShapes import*

###############################################

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = int(length)
        self.width = int(width)
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        return self.length*2 + self.width*2
    def Area(self):
        return  self.length*self.width

###########################################

def PrintALL():

    A = Rectangle("Rectangle", "10", "20", "4",  "4")

### Specs of Rectangle ###
    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter()
    print "This is the area: ", A.Area()
    print "\n"
PrintALL()

Upvotes: 0

TemporalWolf
TemporalWolf

Reputation: 7952

If the shapes won't change, they don't need to be functions:

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        super(Rectangle,self).__init__()
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        self.perimeter = length*2 + width*2
        self.Area = length*width

Will work with the code you wrote. You don't need a function, as you can do the math for the perimeter and Area when it's initialized.

Upvotes: 1

feedMe
feedMe

Reputation: 3727

You could use a return value in the perimeter function:

def perimeter(self):
    return self.length*2 + self.width*2

Then call A.perimeter() instead of A.perimeter.

print "This is the perimeter: ", A.perimeter()

Likewise for Area.

def Area(self):
    return self.length*self.width


print "This is the area: ", A.Area()

EDIT: Sorry, I rushed my answer and didn't bother checking it. Here is a working replacement for the Rectangle class and PrintALL() function. I also edited above.

It is better if you pass number types (not strings) to the function and you can avoid rounding errors by using floats instead of integers.

class Rectangle(TwoDShapes):
    def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
        self.nameOfShape = "Rectangle"
        self.length = length
        self.width = width
        self.numberofSides = 4
        self.numberOfVertices = 4
        super(Rectangle,self).__init__()

    def perimeter(self):
        return self.length*2.0 + self.width*2.0
    def Area(self):
        return self.length*self.width

def PrintALL():

    A = Rectangle("Rectangle", 10.0, 20.0, 4.0, 4.0)

    print "This is the name of the shape: ", A.nameOfShape
    print "This is the length: ", A.length
    print "This is the width: ", A.width
    print "This is the number of side: ", A.numberofSides
    print "This is the number of vertice: ", A.numberOfVertices
    print "This is the perimeter: ", A.perimeter()
    print "This is the area: ", A.Area()
    print "\n"
PrintALL() 

OUTPUT:

This is the name of the shape:  Rectangle
This is the length:  10.0
This is the width:  20.0
This is the number of side:  4
This is the number of vertice:  4
This is the perimeter:  60.0
This is the area:  200.0

Upvotes: 1

Related Questions