How to define "exec" method on a class object? Get syntax error due to built in command

Chris Angelico rosuav at gmail.com
Mon Mar 25 21:43:45 CET 2013


On Tue, Mar 26, 2013 at 7:28 AM, Kyle <stalkernew at gmail.com> wrote:
> I am using swig to generate our CLI for TCL and Python. In this CLI, we have a subcommand "exec" that is failing to compile in the python case. There seems to be some built-in python command "exec" which is giving a syntax error in the .py file generated by swig when I try to import it:
>
>    def exec(*args): return _wbt_daemon.dm_cli_exec(*args)

In Python 2, exec is a keyword, so you can't do that. In Python 3,
exec is simply a built-in function, so it'd work fine. Technically you
can get around the problem in 2.x with setattr/getattr, but that may
not really be all that useful...

    def _exec(*args): return _wbt_daemon.dm_cli_exec(*args)
...

setattr(dm_cli,"exec",dm_cli._exec)

Tested on 2.6 for Windows (I really ought to get myself a 2.7, maybe
when 2.7.4 gets released I'll grab it).

ChrisA



More information about the Python-list mailing list