Python values with units -


I need to keep track of units on float and int value on python, but I do not want to use an external package Quantity or like others, because I do not need to work on values. Instead, what I have to do is to be able to define the floats and the interses, which has an entity attribute (and I think this simple thing A new dependency for Wants to add o). I tried to do this:

  class float (float): __oldinit__ = float .__ init__ def __init __ (self, * args, ** kwargs): If kwargs in 'unit': Self.unit = kwargs .pop ('unit') itself .__ old thing __ (* args, ** kwargs)  

but it does not work at all:

< In pre> [37]: A = floatindt (1., unit = 1) ----------------------------- ------ ---------------------------------------- TypeError traceback (most Recent call final) / user / tom /

You might be looking for something like this:

  Class unitflot (float): def __new __ (self, value, unit = none): return float .__ new__ (self, value) def __init __ (self, value, unit = none): self.unit = Unit x = UnitFloat 35.5, "cm") y = unitflat (42.5) print x print x.unit print y print y.unit print x + y  

yield:

< Pre> 35.5 cm 42.5 None 78.0

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -