一种方法是将其拆分为两个类,并在子类中进行参数修改:
from typing import NamedTuple, Optional
class FormatSpecBase(NamedTuple):
"""Represents a string that conforms to the [Format Specification
Mini-Language][1] in the string module.
[1]: https://docs.python.org/3/library/string.html#formatspec
"""
fill: Optional[str]
align: Optional[str]
sign: Optional[str]
alt: Optional[str]
zero: Optional[int]
width: Optional[int]
comma: Optional[str]
decimal: Optional[str]
precision: Optional[int]
type: str
def join(self):
return ''.join('{!s}'.format(s) for s in self if s is not None)
def __format__(self, format_spec):
try:
return format(self.join(), format_spec)
except (TypeError, ValueError):
return super().__format__(format_spec)
class FormatSpec(FormatSpecBase):
__slots__ = ()
def __new__(cls, fill, align, sign, alt, zero, width, comma, decimal, precision, type):
to_int=lambda x: int(x) if x is not None else x
zero=to_int(zero)
width=to_int(width)
precision=to_int(precision)
return super().__new__(cls, fill, align, sign, alt, zero,
width, comma, decimal, precision, type)
我并不太关心这种方法,但至少它比“旧方式”更具可读性(即使它仍然需要挂起__slots__废话).
另一种方式是工厂:
def MakeFormatSpec(cls, fill, align, sign, alt, zero,
width, comma, decimal, precision, type):
to_int=lambda x: int(x) if x is not None else x
zero=to_int(zero)
width=to_int(width)
precision=to_int(precision)
return FormatSpec(fill, align, sign, alt, zero,
width, comma, decimal, precision, type)
fspec = MakeFormatSpec(*parse_format_spec(some_format_spec_string))
……或工厂方法:
@classmethod
def make(cls, fill, align, sign, alt, zero, width, comma, decimal, precision, type):
to_int=lambda x: int(x) if x is not None else x
zero=to_int(zero)
width=to_int(width)
precision=to_int(precision)
return cls(fill, align, sign, alt, zero,
width, comma, decimal, precision, type)
fspec = FormatSpec.make(*parse_format_spec(some_format_spec_string))
然而,与简单地做到相比,这些都非常笨重:
fspec = FormatSpec(*parse_format_spec(some_format_spec_string))