where is best place for import statements?

Peter Otten __peter__ at web.de
Sun Oct 26 08:08:37 EST 2003


Graham Ashton wrote:

> On Thu, 23 Oct 2003 21:27:37 +0200, Peter Otten wrote:
> 
>> Literal answer: put
>> 
>> from math import *
>> 
>> into myshapes.py, and access variables and functions like so:
> 
>> import myshapes
>> myshapes.pi
>> myshapes.sin(alpha)
> 
> Why on earth would you want to do that? If somebody wants to get the
> value of PI why would you want them to use anything other than math.pi
> directly? I realise you might be using this as an example of what you
> *could* do, but I don't think it's good practice.

Definitely not. I should have used a stronger disclaimer than "literal
answer".

I have a "builtins" module for the interpreter (and a few quick and dirty
scripts) and always use it like that:

from misc import *

As the origin of the imported symbols is thus hidden, there is no potential
for confusion that exceeds the general star import.

> If I was reading code and saw myshapes.sin, and I new that math.sin
> existed (which I do as it's in the standard library), I would
> naturally assume that it was either an alternative implementation
> (which I wouldn't trust), or that it did something else entirely.

You are stressing the universality of sin() a bit much for my taste. Did you
know that Python is shipped with two flavours of trigonometrical functions
(float and complex)? I think Numeric has yet another one. So you could
think of it this way: Let myshapes pick the appropriate sin (pun not
intended but welcome) for me.

Again, as I think I made clear in my previous post, if I were to implement
myshapes, I would strive to hide sin() as an implementation detail.

Peter








More information about the Python-list mailing list