Two locations for module struct ?

Peter Otten __peter__ at web.de
Fri Nov 14 11:51:35 EST 2014


ast wrote:

> In module wave there is a sub module struct.
> You can call function pack() with:
> 
> import wave
> val = wave.struct.pack(...)
> 
> but the same function can be called with:
> 
> import struct
> val = struct.pack(...)
> 
> Is it exactly the same module in both location ?

You can answer that yourself:

>>> import struct, wave
>>> struct is wave.struct
True

> Why putting struct in two places ?

Assumming your code is part of a module called mymodule.py

> import wave
> val = wave.struct.pack(...)

> import struct
> val = struct.pack(...)

you can now also do

import mymodule
val = mymodule.struct.pack(...)

or even

val = mymodule.wave.struct.pack(...)

Do you see the pattern? You should ;)




More information about the Python-list mailing list