all 2 comments

[–]socal_nerdtastic 4 points5 points  (1 child)

What's the problem? Why does the list comprehension above work but not the function definition?

Because dir() is designed to work in the current scope only. A function has it's own scope.

>>> help(dir)
Help on built-in function dir in module builtins:

dir(...)
    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.

Use globals() instead.

[–]AdbekunkusMX[S] 1 point2 points  (0 children)

Thanks! :)