Reputation: 1
I am getting an Attribute error on the main(). Can someone please help me out? This is my class:
import math
class Cylinder2():
def __init__(self, cylRadius = 1, cylHeight = 1):
self.__radius = cylRadius
self.__height = cylHeight
def getPerimeter(self):
return (2 * math.pi * self.__radius)
def getEndArea(self):
return (math.pi * (self.__radius ** 2))
def getSideArea(self):
return (2 * (math.pi * (self.__radius * (self.__height))))
def getSurfaceArea(self):
return (2 * (math.pi * (self.__radius) * (self.__height + self.__radius)))
def getVolume(self):
return (math.pi * (self.__radius ** 2) * self.__height)
def setRadius(self, r):
self.__radius = r
def setHeight(self, h):
self.__height = h
def getRadius(self):
return self.__radius
def getHeight(self):
return self.__height
This is the main():
from CylinderModule2 import *
def main():
bottle = Cylinder2(4, 8)
bottle.setRadius(2)
bottle.setHeight(4)
print("The radius and height of the bottle are: ", bottle.__radius, " and ", bottle.__height)
print("The perimeter of the bottle end circle is", ("%.2f" % bottle.getPerimeter()))
print("The end circle area of the bottle is ", ("%.2f" % bottle.getEndArea()))
print("The side area of the bottle is ", ("%.2f" % bottle.getSideArea()))
print("The total surface of the bottle is ", ("%.2f" % bottle.getSurfaceArea()))
print("The volume of the bottle is ", ("%.2f" % bottle.getVolume()))
main()
This is the error I get: print("The radius and height of the bottle are: ", bottle._radius, " and ", bottle._height) AttributeError: 'Cylinder2' object has no attribute '__radius'
Upvotes: 0
Views: 165
Reputation: 179
bottle.__radius
and bottle.__height
are what can loosely be described as private variables (even though they are not really, you can read about what I mean here: http://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references )
"private" variables can not be directly accessed from outside of the class. In Python, they are denoted by using two underscores (as in __radius).
So what you want to do is using the getters, which are bottle.getRadius()
and bottle.getHeight()
instead.
Upvotes: 1