Код: Выделить всё
class Dummy(object):
def __init__(self):
self.value1 = 'A VALUE'
self.value2 = 'ANOTHER VALUE'
self.value3 = 'THIRD VALUE'
def to_s(self):
# want value1 to be 20 chars
# value2 to be 8 chars
# value3 to be 10 chars
# is something similar to this possible
return '{value1},{value2},{value3}'.format(**vars(self), [20, 8, 10])
def to_s2(self):
# or will I have to reference each explicitly and specify the either padding or slicing?
return '{},{},{}'.format(self.value1.ljust(20), self.value2[:8], self.value3[:10])
Подробнее здесь: https://stackoverflow.com/questions/402 ... rs-or-dict
Мобильная версия