The complex part aligns several table entries and table headers. To do this, you first need to find out how long the record in each column lasts. padcan then be used to add a few spaces to the space.
fields = ["ident", "make", "model", "disp", "power", "luxury"]
max_len = {"name": max(map(len, car_objects)) + 1}
for f in fields:
max_len[f] = max(map(len, [f] + [str(car[f]) for car in car_objects.values()]))
pad = lambda s, f: str(s).ljust(max_len[f])
car_objects pad, .
print pad("", "name") + " ".join(pad(f.upper(), f) for f in fields)
for name, car in car_objects.items():
print pad(name, "name") + " ".join(pad(car[f], f) for f in fields)
, , car_objects Python. , car[f] getattr(c, f) .
: , , . , "", , . . for :
print (" " * 20) + " ".join(f.upper().ljust(10) for f in fields)
, yield:
print name.ljust(20) + " ".join(str(getattr(car, f)).ljust(10) for f in fields)
str.ljust(n) - , , , n. : rjust center. , , - , getattr(<object>, <attribute name>) ( getVariable).
. Python.