Skip to content
This repository was archived by the owner on Dec 20, 2025. It is now read-only.

Consider a more robust exec approach #5

@bernhard-42

Description

@bernhard-42

exec fails with simple (an natural) approaches:

import cadquery as cq

l = 1
w = 2
h = 3

def box():
    return cq.Workplane().box(l, w, h).edges().fillet(0.1)

show(box())

fails with

  File "<string>", line 9, in box
NameError: name 'cq' is not defined

So let's change it (just for the test, I would never program like that):

l = 1
w = 2
h = 3


def box():
    import cadquery as cq

    return cq.Workplane().box(l, w, h).edges().fillet(0.1)


show(box())

Now, this fails with

  File "<string>", line 9, in box
NameError: name 'l' is not defined

exec just doesn't play well with global variables

Of course, this works:

def box(l=1, w=2, h=3):
    import cadquery as cq

    return cq.Workplane().box(l, w, h).edges().fillet(0.1)

show(box())

However I would need to adapt my style of coding - especially for quick hacks - to the limitations of exec.

Maybe there is a more robust way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions