0

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
2
  • 1
    side and area are local variables, not attributes of the return value. You cannot access them outside the function. Commented Jul 29, 2019 at 15:19
  • 2
    I think they are local functions and not variables. But you're right in that they cannot be accessed from outside the function. Commented Jul 29, 2019 at 15:21

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.