Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80772

Re: Two locations for module struct ?

From Peter Otten <__peter__@web.de>
Subject Re: Two locations for module struct ?
Date 2014-11-14 17:51 +0100
Organization None
References <54661ba8$0$2906$426a74cc@news.free.fr>
Newsgroups comp.lang.python
Message-ID <mailman.15823.1415983912.18130.python-list@python.org> (permalink)

Show all headers | View raw


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 ;)

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Two locations for module struct ? "ast" <nomail@invalid.com> - 2014-11-14 16:11 +0100
  Re: Two locations for module struct ? Chris Angelico <rosuav@gmail.com> - 2014-11-15 03:24 +1100
  Re: Two locations for module struct ? Peter Otten <__peter__@web.de> - 2014-11-14 17:51 +0100
    Re: Two locations for module struct ? "ast" <nomail@invalid.com> - 2014-11-14 19:12 +0100
  Re: Two locations for module struct ? Terry Reedy <tjreedy@udel.edu> - 2014-11-14 16:47 -0500

csiph-web