Steven D'Aprano wrote:
> Arguably a better solution would be to do this:
>
> seconds = td.days * 24*60*60 + td.seconds
if you're targeting an audience that cannot figure out what the
expression does based on the names of the result and the names of the
attributes, chances are that 24*60*60 won't make a lot more sense either.
in such cases, you're better off using a named constant.
(that also allows you to change the value, would the need arise.)
why stop at 24*60*60, btw? if you're really serious about working from
basic units, you should probably make it
24*60*60*9192631770*BASIC_SECOND_UNIT
:::
and yes, if you want the CPython compilator to do the multiplication for
you, put parentheses around the subexpression:
>>> def get_seconds(td):
... return td.days * (24*60*60) + td.seconds
...
>>> import dis
>>> dis.dis(get_seconds)
2 0 LOAD_FAST 0 (td)
3 LOAD_ATTR 0 (days)
6 LOAD_CONST 4 (86400)
9 BINARY_MULTIPLY
10 LOAD_FAST 0 (td)
13 LOAD_ATTR 1 (seconds)
16 BINARY_ADD
17 RETURN_VALUE
</F>
--
http://mail.python.org/mailman/listinfo/python-list