Python print语句更改代码行为

时间:2017-01-31 17:09:32

标签: python

如果我从命令行(使用python pyCode.py)按原样运行下面的代码,然后键入"继续",它将在行peakIndexes = imageSlopeArray.argmax(axis=1)上出错。但是,在pdb.set_trace()#;print(imageSlopeArray)行上取消注释print语句会导致行rowStart = xIntercept+avgSlope*curRow+5上出错。无论print编辑的是什么,都会发生此错误。 据我所知,分号和打印语句都不会对代码执行产生任何影响。这是一个错误吗?我对代码正在做什么或它抛出的实际错误不感兴趣。当print语句放在自己的行上时,问题就消失了。这不是我第一次使用print语句导致代码行为发生变化。

我正在运行Python 2.7.13版 Fedora在Windows 10上的Oracle虚拟机版本5.1.14 r112924中发布25版。

import numpy as np
import pdb

def errorFunction():
    imageRGBArray = np.array([[[ 86.,  87.,  44.],
    [ 86.,  86.,  44.],
    [ 87.,  87.,  44.],
    [ 86.,  87.,  44.],
    [ 87.,  88.,  44.],
    [ 88.,  88.,  45.],
    [ 87.,  88.,  44.],
    [ 87.,  87.,  44.],
    [ 89.,  89.,  46.],
    [ 90.,  91.,  47.]],

   [[ 86.,  87.,  45.],
    [ 87.,  86.,  45.],
    [ 87.,  87.,  46.],
    [ 89.,  89.,  45.],
    [ 88.,  88.,  45.],
    [ 88.,  89.,  44.],
    [ 88.,  89.,  44.],
    [ 88.,  88.,  44.],
    [ 89.,  88.,  45.],
    [ 88.,  89.,  45.]]])


    #Convert to grayscale
    imageGryArray = np.mean(imageRGBArray, axis = 2)
    [imageHeight,imageWidth] = imageGryArray.shape

    #Detect the location of the edge
    imageSlopeArray = imageGryArray[:,2:] - imageGryArray[:,:-2]
    pdb.set_trace()#;print(imageSlopeArray)
    peakIndexes = imageSlopeArray.argmax(axis=1)
    [avgSlope, xIntercept] = np.polyfit(range(peakIndexes.shape[0]), peakIndexes, 1)

    rowStart = xIntercept+avgSlope*curRow+5

errorFunction()

使用注释的print语句跟踪代码:

(Pdb) continue
Traceback (most recent call last):
  File "error.py", line 40, in <module>
errorFunction()
  File "error.py", line 35, in errorFunction
    peakIndexes = imageSlopeArray.argmax(axis=1)
NameError: global name 'curRow' is not defined

使用未注释的print语句跟踪代码:

(Pdb) continue
[[ 0.33333333  0.33333333  0.33333333  1.33333333  0.         -1.
   1.66666667  3.33333333]
 [ 0.66666667  1.66666667  0.33333333 -0.66666667  0.         -0.33333333
   0.33333333  0.66666667]]
Traceback (most recent call last):
  File "error.py", line 40, in <module>
    errorFunction()
  File "error.py", line 38, in errorFunction
    rowStart    = xIntercept+avgSlope*curRow+5
NameError: global name 'curRow' is not defined

删除了pdb.set_trace()的代码的回溯以及未注释的print语句:

[[ 0.33333333  0.33333333  0.33333333  1.33333333  0.         -1.
   1.66666667  3.33333333]
 [ 0.66666667  1.66666667  0.33333333 -0.66666667  0.         -0.33333333
   0.33333333  0.66666667]]
Traceback (most recent call last):
  File "error.py", line 40, in <module>
    errorFunction()
  File "error.py", line 38, in errorFunction
    rowStart    = xIntercept+avgSlope*curRow+5
NameError: global name 'curRow' is not defined

0 个答案:

没有答案
相关问题