Is this possible in Python?

alainpoint at yahoo.fr alainpoint at yahoo.fr
Wed Mar 15 07:28:51 CET 2006


Steven D'Aprano wrote:

>
> Doesn't work for me either:
>
> >>> def magic(arg):
> ...     import inspect
> ...     return inspect.stack()[1][4][0].split("magic")[-1][1:-1]
> ...
> >>> magic(3+4)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 3, in magic
> TypeError: unsubscriptable object
>
>
> Kay gets an AssertionError, I get a TypeError. I think describing it as
> "proof of concept" is rather optimistic.
>
> Here is the inspect.stack() I get:
>
> [(<frame object at 0x825d974>, '<stdin>', 2, 'magic', None, None),
> (<frame object at 0x8256534>, '<stdin>', 1, '?', None, None)]
>
>
> --
> Steven.

You just proved what i was saying: this is no robust code. It
apparently does not work from the command line. It has problems with
comments on the same line. It might have problems with multi-line
arguments, etc ....
Please feel free to improve the above function and make it robust.
(meanwhile, i removed the SOLUTION FOUND from the subject title, until
you come up with such a robust version ;-))
Alain




More information about the Python-list mailing list