Hướng dẫn fixed length string python

I know this is a bit of an old question, but I've ended up making my own little class for it.

Might be useful to someone so I'll stick it up. I used a class variable, which is inherently persistent, to ensure sufficient whitespace was added to clear any old lines. See below:

2021-03-02 update: Improved a bit - when working through a large codebase, you know whether the line you are writing is one you care about or not, but you don't know what was previously written to the console and whether you want to retain it.

This update takes care of that, a class variable you update when writing to the console keeps track of whether the line you are currently writing is one you want to keep, or allow overwriting later on.

class consolePrinter[]:
'''
Class to write to the console

Objective is to make it easy to write to console, with user able to 
overwrite previous line [or not]
'''
# -------------------------------------------------------------------------    
#Class variables
stringLen = 0    
overwriteLine = False
# -------------------------------------------------------------------------    
    
# -------------------------------------------------------------------------
def writeline[stringIn, overwriteThisLine=False]:
    import sys
    #Get length of stringIn and update stringLen if needed
    if len[stringIn] > consolePrinter.stringLen:
        consolePrinter.stringLen = len[stringIn]+1
    
    ctrlString = "{:

Chủ Đề