python namedtuple默认值_python – 在创建实例之前修改输入.NamedTuple的参数

一种方法是将其拆分为两个类,并在子类中进行参数修改:

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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值