0

Related question here, but I'm not able to translate the solution (i.e. use inspect) in actual code. In addition, the cited question is 5 years old and new methodologies might have become available.

locals() is concise and great, but it returns a dictionary that is affected by what happens in the local environment. However, the solution should be independent on what's going on in the local environment.

def foo(a=1):
    return locals()
foo()  # returns {'a': 1}


def bar(a=1):
    a = 2
    return locals()
bar()  # returns {'a': 2}


def solution(a=1):
    a = 2
    return # DO: RETURN SOMETHING
solution()  # should returns {'a': 1}
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
MLguy
  • 1,776
  • 3
  • 15
  • 28
  • 2
    Whats wrong with `inspect.signature`? – heemayl Mar 14 '18 at 12:35
  • It requires to pass a callable – MLguy Mar 14 '18 at 13:06
  • what's wrong with passing a callable? – Henry Heath Mar 14 '18 at 16:29
  • I'll need to get signature in the same way for many asynchronous callback methods. If I had to specify the callable for every method, I could mistakenly provide the wrong callable. You could argue that unit-testing will avoid this sort of errors. I argue that the solution to this question will allow to reduce the number of tests and will make the mode cleaner since there is strictly less logic (no callables). – MLguy Mar 14 '18 at 18:04

0 Answers0