Search code examples
pythonclass-methodglobal-scope

Python How to declare a class inside a method when it is required to be global


I have the following function:

def create_figures():
    circle = figure()
    square = figure()

Then I have the following program:

create_figures()
circle.get_area()

The get_area() method is an error because circle doesn't exist in that scope, so my question is, how do I declare the circle class to be global in a nice pythonian way? I was thinking about declaring it at the top of the program like:

circle = figure()

and then just overwrite it in the create_figures() method.


Solution

  • Please do correct if I misunderstood your question but is this what you are looking for?

    def create_figures():
        circle = figure()
        square = figure()
        return circle,square
    

    And

    circle,square = create_figures()
    circle.get_area()
    

    Edit: Removed a typo