在Python中,如何用命名为tuple的方法表示类?

在Python中,如何用命名为tuple的方法表示类?,python,class,namedtuple,repr,Python,Class,Namedtuple,Repr,我最初使用命名元组编写了一些代码: 从集合导入namedtuple Count=namedtuple('Count','input\u number multiple\u of Count rements') def get_count(输入_编号:int,整数的倍数)->计数: ''' >>>获取_计数(100,10) 计数(输入数=100,倍数=10,计数=10,余数=0) >>>获取_计数(5,6) 计数(输入数=5,倍数=6,计数=0,余数=5) >>>获取计数(999,90) 计数(输

我最初使用命名元组编写了一些代码:

从集合导入namedtuple
Count=namedtuple('Count','input\u number multiple\u of Count rements')
def get_count(输入_编号:int,整数的倍数)->计数:
'''
>>>获取_计数(100,10)
计数(输入数=100,倍数=10,计数=10,余数=0)
>>>获取_计数(5,6)
计数(输入数=5,倍数=6,计数=0,余数=5)
>>>获取计数(999,90)
计数(输入数=999,倍数=90,计数=11,余数=9)
'''
计数=输入数量//多个
余数=输入数量%的倍数
返回计数(
输入编号=输入编号,
多个=多个,
计数=计数,
余数=余数
)
注意:打印对象时,其格式为:例如,计数(输入数=100,倍数=10,计数=10,余数=0)

是否有一种Pythonic/最佳实践方法以同样的方式表示普通类?

然后,我想合并python3.6+的键入功能,发现我无法使用集合中的namedtuple实现这一点,因此我将Count类编辑为以下内容:

输入import NamedTuple
类计数(命名为整数):
输入号码:int
整数的倍数
计数:int
余数:int
我不太满意需要两个功能,因此我开始使用新功能:

class GetCount():
'''
>>>GetCount(100,10)
GetCount(输入数量=100,倍数=10,计数=10,余数=0)
>>>GetCount(5,6)
GetCount(输入数=5,倍数=6,计数=0,余数=5)
>>>GetCount(999,90)
GetCount(输入数=999,倍数=90,计数=11,余数=9)
'''
定义初始化(自身,输入编号:int,多个:int):
self.input\u number:int=input\u number
self.multiple\u of:int=multiple\u of
self.count:int=input\u number//multiple\u of
self.rements:int=input\u number%的倍数

以我期望的方式获得输出的最佳方法是什么(请参阅doctests以获得期望的输出)。

该问题的解决方案如下:

class GetCount():
'''
>>>GetCount(100,10)
GetCount(输入数量=100,倍数=10,计数=10,余数=0)
>>>GetCount(5,6)
GetCount(输入数=5,倍数=6,计数=0,余数=5)
>>>GetCount(999,90)
GetCount(输入数=999,倍数=90,计数=11,余数=9)
'''
定义初始化(自身,输入编号:int,多个:int):
self.input\u number:int=input\u number
self.multiple\u of:int=multiple\u of
self.count:int=input\u number//multiple\u of
self.rements:int=input\u number%的倍数
定义报告(自我):
返回f“{self.\uuuuu class.\uuuuuu name.{uuuuu}({,'.join([f'{k}={v}表示k,v表示self.\uuu dict.\uuuuuu.items())})”
通过运行以下命令,您可以看到提供的测试通过:

导入doctest
doctest.testmod(verbose=True)