动态方式在python中多次打印同一行

时间:2016-01-06 22:18:50

标签: python python-2.7 printing

我有一些操作,我想在完成每个操作后显示一个分隔线。 (为了更好的可读性)

我的代码:

print line

这正是我想要的。但这里的问题是我有100个操作。之后,如果我想停止显示行,我必须从任何地方删除HRESULT hr = S_OK; UINT createDeviceFlags = 0; #ifdef _DEBUG createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; #endif D3D_DRIVER_TYPE driverTypes[] = { D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP, D3D_DRIVER_TYPE_REFERENCE, }; UINT numDriverTypes = ARRAYSIZE(driverTypes); D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0, }; UINT numFeatureLevels = ARRAYSIZE(featureLevels); DXGI_SWAP_CHAIN_DESC sd; ZeroMemory(&sd, sizeof(sd)); sd.BufferCount = 1; sd.BufferDesc.Width = _WindowWidth; sd.BufferDesc.Height = _WindowHeight; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.RefreshRate.Numerator = 60; sd.BufferDesc.RefreshRate.Denominator = 1; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.OutputWindow = _hWnd; sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; sd.Windowed = TRUE; for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++) { _driverType = driverTypes[driverTypeIndex]; hr = D3D11CreateDeviceAndSwapChain(nullptr, _driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &sd, &_pSwapChain, &_pd3dDevice, &_featureLevel, &_pImmediateContext); if (SUCCEEDED(hr)) break; }

另一个解决方案可能是管理一些全局标志以检查是否显示行。

还有其他简单的&amp;动态解决这个问题?

2 个答案:

答案 0 :(得分:2)

你可能已经陷入了复制的陷阱,在几百年的时间内粘贴相同的东西。我无法评论和询问,但如果你# do somethings是相同的,你可以这样做:

if __name__ == "__main__":
    line = '-' * 100
    for _ in range(<how many times>):
        print line
        # do something

如果它们不相同,它们应该是函数,因为函数之外不应该有太多代码。例如,

if __name__ == '__main__':
    line = '-' * 100

    print line
    function_a()

    print line
    function_b()

    print line
    function_c()

    # etc

变为:

if __name__ == '__main__':
    line = '-' * 100

    for function in (function_a, function_b,  # etc
                     function_c):
        print line
        function()

答案 1 :(得分:1)

在顶部添加一个布尔变量并用它来控制打印语句:

print_lines = True  # Change to False when you no longer want to print
if print_lines:
    print line