Usefulness of python 2 input(), eval(), exec...

A forum for general discussion of the Python programming language.

Usefulness of python 2 input(), eval(), exec...

Postby mckryall » Sun Mar 30, 2014 8:19 pm

stranac wrote:input() will try to interpret anything you enter as python code. raw_input() gets a string from user.

Wait, seriously? As code? that helps. A lot. Also, how can you run code from a string (variable code that the program edits with other variables)?
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Text Based Game - Please Review!

Postby stranac » Sun Mar 30, 2014 8:23 pm

mckryall wrote:Wait, seriously? As code? that helps. A lot.

That's very rarely useful, actually. So rarely, that in python 3, python 2's input doesn't even exist.
mckryall wrote:Also, how can you run code from a string (variable code that the program edits with other variables)?

eval or exec, but those are also very rarely useful.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1133
Joined: Thu Feb 07, 2013 3:42 pm

Re: Text Based Game - Please Review!

Postby mckryall » Sun Mar 30, 2014 8:25 pm

So, you could rewrite the python interpreter with just this:
Code: Select all
try:
    while True:
        input(">>> ")
except KeyboardInterrupt:
    print "[1]+   Stopped                 python"

Edit:
That doesn't work with operators like "=" or with functions.
Last edited by mckryall on Sun Mar 30, 2014 8:31 pm, edited 1 time in total.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Text Based Game - Please Review!

Postby mckryall » Sun Mar 30, 2014 8:26 pm

Also, I meant useful as in the fact that there is a way to run dynamic code without the interpreter.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Text Based Game - Please Review!

Postby stranac » Sun Mar 30, 2014 8:38 pm

You can emulate the interactive interpreter(if you really want to) with:
Code: Select all
import code
code.interact()

mckryall wrote:Also, I meant useful as in the fact that there is a way to run dynamic code without the interpreter.

I don't see how that is useful... if you want to run code interactively, might as well use the interactive interpreter.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1133
Joined: Thu Feb 07, 2013 3:42 pm

Re: Text Based Game - Please Review!

Postby mckryall » Sun Mar 30, 2014 9:14 pm

Say you have a list. It is a list of characters/numbers. You have to do things to this list, things where both the nature and the number of depend on the list. You could create an if statement with nature * number possibilities, or you could create a string and use operators such as "'str' * num" and algorithms similar to "str = 'DoStuff(%d,%d,%s)' % (num1, num2, str)"
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Text Based Game - Please Review!

Postby mckryall » Sun Mar 30, 2014 9:22 pm

Also, it's useful, when testing code and trying to figure out the best way to interact with something, to emulate the interpreter at the end and try a few different methods, without leaving the original Python instance.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Usefulness of python 2 input(), eval(), exec...

Postby stranac » Sun Mar 30, 2014 9:57 pm

mckryall wrote:Say you have a list. It is a list of characters/numbers. You have to do things to this list, things where both the nature and the number of depend on the list. You could create an if statement with nature * number possibilities, or you could create a string and use operators such as "'str' * num" and algorithms similar to "str = 'DoStuff(%d,%d,%s)' % (num1, num2, str)"

Ok, I guess it might be useful in cases like that, although I never had need for anything like that, and I've been programming for quite some time...
The only time I can think of possibly needing something like that would be when testing stuff, but that's better done in the interactive prompt.

If you have a more specific example, I'd like to hear about it.
mckryall wrote:Also, it's useful, when testing code and trying to figure out the best way to interact with something, to emulate the interpreter at the end and try a few different methods, without leaving the original Python instance.

python -i is much more useful for stuff like that.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1133
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests