Problems about exec and try-except

475 views
Skip to first unread message

Lei

unread,
Dec 19, 2014, 12:05:28 PM12/19/14
to spyd...@googlegroups.com
Hi experts,

I want got a py-file named as try.py

The content is

--------------------------------

import sys


def cal(init_code):

    try:

        exec(init_code)

    except:

        print('OMG')

    try:

        if 'A0=' not in init_code:

            print('OMG')

            A0=A

            print('A0=',A0)

    except NameError as e:

        print(e)


init_code = ''

for statement in sys.argv[1:]:

    init_code += statement + '\n'


cal(init_code)

--------------------------------


When I run it via command line in Spyder, IPython console, (I am using python 3.4)

I got error:

--------------------------------------------------------

In[1]: !python try.py A=10 p=5 n=730

OMG

name 'A' is not defined

--------------------------------------------------------

My idea is that the first try does not work porperly. Because in the second try, the code "print('OMG')" works. But the code "A0=A" does not work.

Could you please help me with this?

Thanks in advance!

Adrian Klaver

unread,
Dec 19, 2014, 6:55:50 PM12/19/14
to spyd...@googlegroups.com
No I am pretty sure it is the first try that is throwing an exception
and that is why A is never defined. That is the issue with a naked
except, it will catch anything and you really don't know what failed. If
you want to find out what is happening do:

except as e:
print(e)

Note the 'as' this is way exceptions should be handled now. except, e is
deprecated. See here:

https://docs.python.org/2/tutorial/errors.html

The bigger question is what are you trying to do?

And why not do it in interest.py, which the above seems to be based on?


Lastly, I would really suggest going through the Python tutorial:

https://docs.python.org/3/tutorial/

>
> Could you please help me with this?
>
> Thanks in advance!
>


--
Adrian Klaver
adrian...@aklaver.com

Lei

unread,
Dec 20, 2014, 6:03:55 AM12/20/14
to spyd...@googlegroups.com
Thanks a lot for your reply!

I tried your suggestions, but it did not work on my computer.

1. I think the except following the first try are not performed, since I added a code in the except block, it was not shown in the output.

2. The "exec(init_code)" produced a "None" value, since I modified this code to
-------------------------------------
print(exec(init_code))
-------------------------------------
In the output, the word "None" showed up. So I think, the reason the whole try.py file not working is this code "exec(init_code)" just produces None.

3. For the except code following the first try, I changed to 
--------------------------------------
except as e
--------------------------------------
but, my computer said it is invalid syntax.

4. The interest.py file contains much more things that are not directly related to this question, so I think it would be clearer to make a concentrated file.

5. The Python tutorial is wonderful, but it is too general. Not specific for my question. I have already tried to find my answers in this tutorial, but I could not find it.

Adrian Klaver

unread,
Dec 20, 2014, 10:11:57 AM12/20/14
to spyd...@googlegroups.com
On 12/20/2014 03:03 AM, Lei wrote:
> Thanks a lot for your reply!
>
> I tried your suggestions, but it did not work on my computer.
>
> 1. I think the except following the first try are not performed, since I
> added a code in the except block, it was not shown in the output.
>
> 2. The "exec(init_code)" produced a "None" value, since I modified this
> code to
> -------------------------------------
> print(exec(init_code))
> -------------------------------------
> In the output, the word "None" showed up. So I think, the reason the
> whole try.py file not working is this code "exec(init_code)" just
> produces None.

Yes. I stay away from exec as much as possible, to 'black box'. I would
take a look at using argparse to catch the arguments passed in;

https://docs.python.org/3/howto/argparse.html#id1


>
> 3. For the except code following the first try, I changed to
> --------------------------------------
> except as e
> --------------------------------------
> but, my computer said it is invalid syntax.

Which is correct:( My mistake, you cannot do that with a bare except as
it turns out. I should have tested before suggesting it.

>
> 4. The interest.py file contains much more things that are not directly
> related to this question, so I think it would be clearer to make a
> concentrated file.
>
> 5. The Python tutorial is wonderful, but it is too general. Not specific
> for my question. I have already tried to find my answers in this
> tutorial, but I could not find it.


FYI this list is really for questions related to Spyder, not general
Python questions. If you want to continue off-list I will help where I can.

>
>
>
>



--
Adrian Klaver
adrian...@aklaver.com

Lei

unread,
Dec 21, 2014, 3:43:31 AM12/21/14
to spyd...@googlegroups.com
Thanks for your help. Sorry for posting any unrelated questions. The reason I post in this group is that at beginning I did not know where is the problem of my codes, I guessed that my path settings in Spyder was not correct.
Reply all
Reply to author
Forward
0 new messages