1

I am experimenting with writing more forgiving/ flexible functions and would like to know if it is possible to access the input arguments of a function as strings, before Python checks for syntax errors, NameErrors, etc, (for the purposes of doing my own input checking first)?

Oster R.
  • 123
  • 3
  • Python can't compile your source-code into byte-code (which is what it interprets later) if there are syntax errors. If your goal is to write code in the same project as one containing syntax errors, it would be impossible. If on the other hand you want to access params before the function handles them, look up [decorators](http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python/1594484#1594484) – inspectorG4dget May 11 '13 at 06:35
  • I am not unfamiliar with decorators but I am not aware of a way to read as strings the inputs of the function being decorated. – Oster R. May 11 '13 at 06:42
  • Oster: You're going to have to explain what you mean by "read as strings". Is `inspect.getargspec()` what you're after? – Pi Delport May 11 '13 at 06:52
  • I guess a partial, related strategy is given in the answers here: http://stackoverflow.com/questions/2270795/getting-the-name-which-is-not-defined-from-nameerror-in-python – Oster R. May 11 '13 at 06:52

1 Answers1

1

No. What you are looking for is sophisticated macro functionality. You can do this in Lisp, but Python (like most languages) does not support it.

If you want, you can preprocess a file and parse it using the ast module. But you would have to do this as a separate step, before you run your Python script.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415