-1

I tried to write a nested function to calculate the area. However, the program showed the name circle is not defined.

Could anyone please help providing a suggestion about this nested function problem? What concept is missing from the below code?

def area(shape, n):
    def circle(radius):
        return 3.14*radius**2
    def square(length):
        return length*length

print(area(circle, 10)   #system return -> name 'circle' is not defined
print(area(square, 5)   #system return -> name 'square' is not defined
4
  • 2
    What concept is missing from the below code? - Binding of names, Resolution of names, Python Scopes and Namespaces. Commented Jun 2, 2022 at 16:04
  • 2
    The concept you're missing is that everything defined inside a function, including nested functions, are local to their container. Commented Jun 2, 2022 at 16:11
  • There shouldn't even be nested functions in this case. Commented Jun 2, 2022 at 16:14
  • Some good Q&A's searching with python functions in a class or module site:stackoverflow.com or python function grouping site:stackoverflow.com or something similar. Commented Jun 2, 2022 at 16:32

4 Answers 4

2

Instead of encapsulating those in a function, put them in a module.

area.py...

def circle(radius):
    return 3.14*radius**2
def square(length):
    return length*length

Then use them by importing area :

other.py

import area
q = area.circle(10)
z = area.square(5)

another.py

import area
shape = 'circle'
q = getattr(area,shape)(10)
shape = 'square'
z = getattr(area,shape)(5)

and.py

from area import circle,square
q = circle(10)
z = square(5)
Sign up to request clarification or add additional context in comments.

1 Comment

It seems that circle should not be implemented as a nested function. I suspect that OP has a spelling error.
0

Circle is only defined in the scope of the area funcion. Functions are just like any other variable, you can read more here.

def circle(radius):
    return 3.14*radius**2

def square(length):
    return length*length

def area(shape, n):
    return shape(n)

print(area(circle, 10)
print(area(square, 5)

Comments

0

Since The functions are nested inside of area. Circle and square won't be recognised outside of the area function. I don't know if you need them to be nested. But you can just put them outside of the area function.

def circle(radius):
    return 3.14*radius**2
def square(length):
        return length*length

def area(shape, n):
    if shape == circle:
        print(circle(n))  

    if shape == square:
        print(square(n))  

area(circle, 20)
area(square, 20)

There are a lot of ways to do this.

def area(shape, n):
    if shape == "circle":
        return 3.14*n**2 

    if shape == "square":
        return n*n 

print(area("circle", 20))
print(area("square", 20))

Comments

0

You can have nested functions, but you can't access them unless they are, for example, returned from that inner scope:

def area():
    def circle(radius):
        return 3.14*radius**2
    def square(length):
        return length*length
    return circle,square

circle, square = area()

print(circle(10))
print(square(5))

Output:

314.0
24

Comments

Your Answer

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.