define a new func on the fly?

Tim Peters tim.one at home.com
Thu Mar 1 22:41:57 EST 2001


[Costas Menico]
> Here is the exact error I get when running it
> 
> >>>> exec("def f():\n\treturn 'hello'\t")
> Traceback (innermost last):
>   File "<interactive input>", line 1, in ?
>   File "<string>", line 2
>     return 'hello'
>                   ^
> SyntaxError: invalid syntax
>
> I've trying all day to get this to work. No luck...

>>> exec "def f():\n\treturn 'hello'\n"
>>> f()
'hello'
>>>

1. exec is a statement, not a function, so you don't need the parens
   (although they shouldn't hurt).

2. You *intended* to end your string with a newline, not a tab.  The
   tab is what yields the SyntaxError.  A good clue about that is the
   caret pointing after the final ' in 'hello' in the error message.





More information about the Python-list mailing list