我需要跟蹤Python中float和int值的單位,但我不想使用像幅度或其他值這樣的外部包,因爲我不需要對這些值執行操作。相反,我所需要的是能夠定義具有單位屬性的浮點數和整數(並且我不想爲這種簡單的事情添加新的依賴項)。我試着這樣做:單位的Python值
class floatwithunit(float):
__oldinit__ = float.__init__
def __init__(self, *args, **kwargs):
if 'unit' in kwargs:
self.unit = kwargs.pop('unit')
self.__oldinit__(*args, **kwargs)
但是,這並不在所有的工作:
In [37]: a = floatwithunit(1.,unit=1.)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/tom/<ipython console> in <module>()
TypeError: float() takes at most 1 argument (2 given)
Any suggestions?
「寫下來,」國王對陪審團說,陪審團熱切地在他們的石板上寫下了三個日期,然後把它們加起來,減少先令和便士的答案。 - 愛麗絲夢遊仙境 - L.卡羅爾 – 2009-12-05 17:48:30