Working with decimals

Joshua Landau joshua at landau.ws
Sun Aug 24 15:12:17 EDT 2014


On 23 August 2014 23:53, Chris Angelico <rosuav at gmail.com> wrote:
> On Sun, Aug 24, 2014 at 8:47 AM, Joshua Landau <joshua at landau.ws> wrote:
>> On 23 August 2014 23:31, Chris Angelico <rosuav at gmail.com> wrote:
>>> I'd say "never" is too strong (there are times when it's right to put
>>> an import inside a function), but yes, in this case it should really
>>> be at the top of the function.
>>
>> But do any of them apply to "import math"?
>
> Yep. If you have only one function that will ever use it, and that
> function often won't ever be called, then putting the import inside
> the function speeds up startup. Anything that cuts down on I/O can
> give a dramatic performance improvement.

>>> python -c "import time; a = time.time(); import math; b = time.time(); print(b-a)"
0.0005981922149658203

*squints eyes*

Is math not already imported by start-up?

>>> However, you won't need the import at all if you let the formatting
>>> function do the rounding for you.
>>
>> Can that floor?
>
> I'm not sure, dig into the format spec and see!

FWIW, I haven't seen something that does so.



More information about the Python-list mailing list