Python:如何在循环中从`eval`调用`print`?

时间:2022-10-30 23:45:52

When I call print from eval:

当我从eval调用print时:

def printList(myList):
    maxDigits = len(str(len(myList)))
    Format = '0{0}d'.format(maxDigits)
    for i in myList:
        eval('print "#{0:' + Format + '}".format(i+1), myList[i]')

it gives an error:

它给出了一个错误:

    print "#{0:01d}".format(i+1), myList[i]
        ^
SyntaxError: invalid syntax

I tried to make use of this, and re-wrote it:

我试图利用这个,并重写它:

def printList(myList):
    maxDigits = len(str(len(myList)))
    Format = '0{0}d'.format(maxDigits)
    for i in myList:
        obj = compile(src, '', 'exec')
        eval('print "#{0:' + Format + '}".format(i+1), myList[i]')

but this complains about the i:

但这抱怨我:

NameError: name 'i' is not defined

P.S. I'm dealing with python2.6

附:我正在处理python2.6

4 个解决方案

#1


7  

You don't need eval:

你不需要eval:

def printList(myList):
    maxDigits = len(str(len(myList)))
    str_format = '#{0:0' + str(maxDigits) + '}'
    for i, elem in enumerate(myList, 1):
        print str_format.format(i), elem

or, as @SvenMarnach noted, you can put even the formatting parameter into one format call:

或者,正如@SvenMarnach所说,你甚至可以将格式化参数放入一个格式调用中:

def printList(myList):
    maxDigits = len(str(len(myList)))
    for i, elem in enumerate(myList, 1):
        print '#{1:0{0}} {2}'.format(maxDigits, i, elem)

#2


10  

You can't eval() a print: eval() is used to evaluate expression, and print is a statement. If you want to execute a statement, use exec(). Check this question for a better explanation:

你不能eval()打印:eval()用于计算表达式,print是一个语句。如果要执行语句,请使用exec()。请查看此问题以获得更好的解释:

>>> exec('print "hello world"')
hello world

Now, you can pass your locals() variables if you want to make accessible the i in the exec:

现在,如果要在exec中使i可访问,则可以传递locals()变量:

>>> i = 1
>>> exec('print "hello world", i', locals())
hello world 1

In addition, in the last test you wrote, you compile() in 'exec' mode, that should give you a tip :)

另外,在你写的最后一个测试中,你在'exec'模式下编译()应该给你一个提示:)

#3


3  

To keep your code while making it shorter and easier to understand:

保持代码的同时缩短代码并使其更容易理解:

def printList(myList):
    # int(math.log10(len(myList))+1) would be the appropriate way to do that:
    maxDigits = len(str(len(myList)))
    for i in myList:
        print "#{0:0{1}d}".format(i+1, maxDigits), myList[i]

#4


1  

The simplistic view is this. Build the format separately from using it. Avoid eval().

简单的观点就是这样。单独构建格式使用它。避免使用eval()。

    format =  "#{0:" + Format + "}"
    print format.format(i+1), myList[i]

Don't make things harder than they need to be. Here's another version that builds the format in one step.

不要让事情变得比他们需要的更难。这是另一个版本,只需一步即可构建格式。

    format = '#{{0:0{0}d}}'.format(maxDigits)
    print format.format(i+1), myList[i]

#1


7  

You don't need eval:

你不需要eval:

def printList(myList):
    maxDigits = len(str(len(myList)))
    str_format = '#{0:0' + str(maxDigits) + '}'
    for i, elem in enumerate(myList, 1):
        print str_format.format(i), elem

or, as @SvenMarnach noted, you can put even the formatting parameter into one format call:

或者,正如@SvenMarnach所说,你甚至可以将格式化参数放入一个格式调用中:

def printList(myList):
    maxDigits = len(str(len(myList)))
    for i, elem in enumerate(myList, 1):
        print '#{1:0{0}} {2}'.format(maxDigits, i, elem)

#2


10  

You can't eval() a print: eval() is used to evaluate expression, and print is a statement. If you want to execute a statement, use exec(). Check this question for a better explanation:

你不能eval()打印:eval()用于计算表达式,print是一个语句。如果要执行语句,请使用exec()。请查看此问题以获得更好的解释:

>>> exec('print "hello world"')
hello world

Now, you can pass your locals() variables if you want to make accessible the i in the exec:

现在,如果要在exec中使i可访问,则可以传递locals()变量:

>>> i = 1
>>> exec('print "hello world", i', locals())
hello world 1

In addition, in the last test you wrote, you compile() in 'exec' mode, that should give you a tip :)

另外,在你写的最后一个测试中,你在'exec'模式下编译()应该给你一个提示:)

#3


3  

To keep your code while making it shorter and easier to understand:

保持代码的同时缩短代码并使其更容易理解:

def printList(myList):
    # int(math.log10(len(myList))+1) would be the appropriate way to do that:
    maxDigits = len(str(len(myList)))
    for i in myList:
        print "#{0:0{1}d}".format(i+1, maxDigits), myList[i]

#4


1  

The simplistic view is this. Build the format separately from using it. Avoid eval().

简单的观点就是这样。单独构建格式使用它。避免使用eval()。

    format =  "#{0:" + Format + "}"
    print format.format(i+1), myList[i]

Don't make things harder than they need to be. Here's another version that builds the format in one step.

不要让事情变得比他们需要的更难。这是另一个版本,只需一步即可构建格式。

    format = '#{{0:0{0}d}}'.format(maxDigits)
    print format.format(i+1), myList[i]