I am create this function
def volumn(value_side):
def side(value_side):
side = value_side+2
return (side)
value_area = side(value_side)
def area(value_area):
area = value_area*4
return(area)
final_result = area(value_area)
return(final_result)
My question is How If I just want to call side function
I have try with this code volumn(3).side(3) but still error int' object has no attribute 'side'
asked Jul 29, 2019 at 15:18
ilham ramadhan
531 gold badge1 silver badge7 bronze badges
2 Answers 2
You can return the inner functions side and area to access them outside
def volumn():
def side(value_side):
side = value_side+2
return side
def area(value_area):
area = value_area*4
return(area)
return side, area
side, area = volumn()
side_value = side(3)
print(side_value)
print(area(side_value))
Or you can use a class to encapsulate the functions and attributes
class Volumn:
def __init__(self, value_side):
self.value_side = value_side
def side(self):
side = self.value_side+2
return side
def area(self):
area = self.side()*4
return area
v = Volumn(3)
print(v.side())
print(v.area())
The output will be
5
20
answered Jul 29, 2019 at 15:26
Devesh Kumar Singh
20.5k5 gold badges25 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You could define these functions in the outer scope to access them.
def side(value_side):
side = value_side+2
return (side)
def area(value_area):
area = value_area*4
return(area)
def volume(value_side):
value_area = side(value_side)
final_result = area(value_area)
return final_result
answered Jul 29, 2019 at 15:29
N Chauhan
3,5152 gold badges9 silver badges23 bronze badges
Comments
lang-py
sideandareaare local variables, not attributes of the return value. You cannot access them outside the function.