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


Groups > comp.lang.python > #80770 > unrolled thread

Two locations for module struct ?

Started by"ast" <nomail@invalid.com>
First post2014-11-14 16:11 +0100
Last post2014-11-14 16:47 -0500
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#80770 — Two locations for module struct ?

From"ast" <nomail@invalid.com>
Date2014-11-14 16:11 +0100
SubjectTwo locations for module struct ?
Message-ID<54661ba8$0$2906$426a74cc@news.free.fr>
Hello

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 ?
Why putting struct in two places ?





[toc] | [next] | [standalone]


#80771

FromChris Angelico <rosuav@gmail.com>
Date2014-11-15 03:24 +1100
Message-ID<mailman.15822.1415982664.18130.python-list@python.org>
In reply to#80770
On Sat, Nov 15, 2014 at 2:11 AM, ast <nomail@invalid.com> 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 ?
> Why putting struct in two places ?

I don't think that's a submodule of wave. It's just the standard
struct module, which presumably wave imported. Use it as struct, not
as wave.struct.

ChrisA

[toc] | [prev] | [next] | [standalone]


#80772

FromPeter Otten <__peter__@web.de>
Date2014-11-14 17:51 +0100
Message-ID<mailman.15823.1415983912.18130.python-list@python.org>
In reply to#80770
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 ;)

[toc] | [prev] | [next] | [standalone]


#80776

From"ast" <nomail@invalid.com>
Date2014-11-14 19:12 +0100
Message-ID<54664621$0$2391$426a74cc@news.free.fr>
In reply to#80772
"Peter Otten" <__peter__@web.de> a écrit dans le message de 
news:mailman.15823.1415983912.18130.python-list@python.org...

>
> Do you see the pattern? You should ;)
>

Understood
thx 

[toc] | [prev] | [next] | [standalone]


#80783

FromTerry Reedy <tjreedy@udel.edu>
Date2014-11-14 16:47 -0500
Message-ID<mailman.15830.1416001658.18130.python-list@python.org>
In reply to#80770
On 11/14/2014 10:11 AM, ast wrote:
> Hello
>
> In module wave there is a sub module struct.

struct is not a documented part of the wave module.

> You can call function pack() with:
>
> import wave
> val = wave.struct.pack(...)

wave imports several other stdlib modules. All are accessible the same 
way.  None are documented or included in wave.__all__.  Some modules, 
typically more recently written or edited, do things like

import struct as _struct

to avoid such leakage of import.  But this is a nuisance to write and 
read.  Best to stick with the documented api.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web