473,799 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

module confusion

Sorry if this is a completely newbie question ...

I was trying to get information about the logging.handler s module, so
I imported logging, and tried dir(logging.han dlers), but got:

AttributeError: 'module' object has no attribute 'handlers'

The only experience I have in modules is os and os.path ... if I do
the same thing, simply import os and then type dir(os.path), it
displays the contents as expected.

So my question is ... why are they different? I mean, in terms of
designing these modules, how would you go about getting a sub-module
in your name space? And on the other side, how would you go about
getting it out?

Thanks!

Oct 2 '07
40 3502
In message <ma************ *************** ***********@pyt hon.org>, Robert
Kern wrote:
Lawrence D'Oliveiro wrote:
>In message <13************ *@corp.supernew s.com>, Steven D'Aprano wrote:
>>What does type(os.path) return when you try it?

It returns the type of the value contained in that variable, of course:
> >>import os
>>os.path = 3
>>type(os.pat h)
<type 'int'>

See, it's just a variable, like any other.

Oooookay. No one is contending that the "os.path" name can't be reassigned
to a different object or that the "os.path" name is somehow different from
any other name in Python. It's not wrong to say that "os is a module"
either, even though you can obviously reassign that name to another
object, too.
It is not the _name_ that is being reassigned, it is the _variable_ that the
name is bound to. All names in Python are bound to variables at all times.
What I meant when I said "os.path is a bit of a weird case" is that, by
default, the object referred to by the name "os.path" (assuming you've
imported the standard library's os module) is another module and that os
itself is a module, not a package like logging is. This is somewhat odd,
because most modules aren't exposed that way. They are either in their own
file and accessed by importing them directly, or they are inside a
package.
That is also true of the module pointed to by os.path. Like any other
non-built-in module, it lives in its own file.

It also helps to keep clear the difference between a "module" and a "module
object". A "module" is the contents of a Python source file, while
a "module object" is a type of in-memory Python object. An "import"
statement generates the latter from the former. See
<http://docs.python.org/ref/types.html>. Admittedly, other parts of the
Python docs do not keep the distinction clear, but the section on "Modules"
in that page does just about do so.

You can't have modules within modules. But there's no reason an attribute of
one module object can't point to another module object. Notice I say "point
to" rather than "contain". There is no sense in which any Python object
can "contain" any other.
Oct 5 '07 #31
Lawrence D'Oliveiro a écrit :
In message <13************ *@corp.supernew s.com>, Steven D'Aprano wrote:
>What does type(os.path) return when you try it?

It returns the type of the value contained in that variable, of course:
Certainly not. You're confusing Python with C. In Python, 'variables'
are *not* labels for memory locations containing values. Period
>>import os
>>os.path = 3
>>type(os.pat h)
<type 'int'>

See, it's just a variable, like any other.
This is bad faith.
>>import os
type(os.pat h)
<type 'module'>

See, this is a name bound to an object, like any other.
Oct 5 '07 #32
On Fri, 05 Oct 2007 19:51:05 +1300, Lawrence D'Oliveiro wrote:
It is not the _name_ that is being reassigned, it is the _variable_ that
the name is bound to. All names in Python are bound to variables at all
times.
I think this is the source of the confusion. Most people don't seem
to share your definition of `variable` above.

To me a `variable` is made of a name, a memory address, a data type, and
a value. In languages like C the address and type are attached to the
name while in Python both are attached to the value.

Ciao,
Marc 'BlackJack' Rintsch
Oct 5 '07 #33
On Fri, 05 Oct 2007 19:51:05 +1300, Lawrence D'Oliveiro wrote:
There is no sense in which any Python object can "contain" any other.
>>L = [1, 2, 3]
2 in L
True
>>L.__contains_ _(3)
True

--
Steven.
Oct 5 '07 #34
On Fri, 05 Oct 2007 00:12:33 -0500, Robert Kern wrote:
This is somewhat odd, because most modules aren't exposed that way. They
are either in their own file and accessed by importing them directly, or
they are inside a package.
Any time you say:

import parrot
in one of your modules, you export parrot to anything that imports your
module. (Unless you take specific steps to prevent it, for instance with
del parrot.)
Just to pick some random examples:
>>import ConfigParser, base64, fileinput
ConfigParser. re
<module 're' from '/usr/lib/python2.5/re.pyc'>
>>base64.stru ct
<module 'struct' from '/usr/lib/python2.5/struct.pyc'>
>>base64.binasc ii
<module 'binascii' from '/usr/lib/python2.5/lib-dynload/binascii.so'>
>>fileinput.s ys
<module 'sys' (built-in)>
>>fileinput.o s
<module 'os' from '/usr/lib/python2.5/os.pyc'>
It's quite common.
--
Steven.
Oct 5 '07 #35
Steven D'Aprano wrote:
On Fri, 05 Oct 2007 00:12:33 -0500, Robert Kern wrote:
>This is somewhat odd, because most modules aren't exposed that way. They
are either in their own file and accessed by importing them directly, or
they are inside a package.

Any time you say:

import parrot
in one of your modules, you export parrot to anything that
subsequently
imports your
module. (Unless you take specific steps to prevent it, for instance with
del parrot.)
or the creation of an __all__ containing an exclusive list of names for
export.
>

Just to pick some random examples:
>>>import ConfigParser, base64, fileinput
ConfigParser .re
<module 're' from '/usr/lib/python2.5/re.pyc'>
>>>base64.struc t
<module 'struct' from '/usr/lib/python2.5/struct.pyc'>
>>>base64.binas cii
<module 'binascii' from '/usr/lib/python2.5/lib-dynload/binascii.so'>
>>>fileinput.sy s
<module 'sys' (built-in)>
>>>fileinput. os
<module 'os' from '/usr/lib/python2.5/os.pyc'>
It's quite common.

OK, I am sort of getting used to the idea that you guys are going to
beat this one to death with a stick, and will still be tossing emails
back and forth to each other while the rest of us are admiring the heat
death of the universe.

So please try and avoid writing anything that will be misconstrued by
newless cloobs unfortunate enough to reach this thread as a result of a
search for meaningful information on Python imports.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline so I couldn't cat it

Oct 5 '07 #36
On 2007-10-05, Lawrence D'Oliveiro <ld*@geek-central.gen.new _zealandwrote:
In message <sl************ ********@FIAD06 .norwich.edu>, Neil Cerutti wrote:
>On 2007-10-03, Lawrence D'Oliveiro <ld*@geek-central.gen.new _zealand>
wrote:
>>In Python, all names _are_ variables. They are not "bound" to
objects. The value of os.path is a pointer. It's implemented
as a pointer, it has all the semantics of a pointer.

No. A pointer is also an iterator.

void duplicate(char *d, const char *s)
{
while (*d++ = *s++)
;
}

So if you can't do pointer arithmetic, then it's not a pointer?
Trying this:

void duplicate(void *d, const void *s)
{
while (*d++ = *s++)
;
}

I get:

test.c: In function 'duplicate':
test.c:3: warning: dereferencing 'void *' pointer
test.c:3: warning: dereferencing 'void *' pointer
test.c:3: error: invalid use of void expression

So you can't do arithmetic or iterate with a void * pointer.
....or dereference.
Does that mean it's not really a pointer?
That's an interesting taxonimical conundrum. If a pointer were
defined by it's semantics, then yes, a void pointer wouldn't be a
pointer. But pointers are defined not by behavior, but by an
ANSI/ISO standard. The term "pointer to void" makes sense if you
think of it as a pointer in an altered, intermediate state.

I suppose you might score a Pyrrhic victory by claiming that
Python identifiers are pointers that don't behave like pointers.
But you claimed the opposite.

--
Neil Cerutti
Oct 5 '07 #37
On Fri, 05 Oct 2007 07:37:34 -0400, Steve Holden wrote:
Steven D'Aprano wrote:
>On Fri, 05 Oct 2007 00:12:33 -0500, Robert Kern wrote:
>>This is somewhat odd, because most modules aren't exposed that way.
They are either in their own file and accessed by importing them
directly, or they are inside a package.

Any time you say:

import parrot
in one of your modules, you export parrot to anything that

subsequently
Well obviously you have to write the module before people import it. I
didn't really think "you must obey the laws of time and space" needed to
be explained.

>imports your
module. (Unless you take specific steps to prevent it, for instance
with del parrot.)

or the creation of an __all__ containing an exclusive list of names for
export.
No.

__all__ only effects names imported with "from module import *", it has
no effect on "import module".

What was that again about avoiding "writing anything that will be
misconstrued by newless cloobs unfortunate enough to reach this thread as
a result of a search for meaningful information on Python imports"?

--
Steven.
Oct 5 '07 #38
Steven D'Aprano wrote:
On Fri, 05 Oct 2007 07:37:34 -0400, Steve Holden wrote:
>Steven D'Aprano wrote:
>>On Fri, 05 Oct 2007 00:12:33 -0500, Robert Kern wrote:

This is somewhat odd, because most modules aren't exposed that way.
They are either in their own file and accessed by importing them
directly, or they are inside a package.
Any time you say:

import parrot
in one of your modules, you export parrot to anything that
subsequently

Well obviously you have to write the module before people import it. I
didn't really think "you must obey the laws of time and space" needed to
be explained.
But a module needn't be fully executed before it's imported.
>
>>imports your
module. (Unless you take specific steps to prevent it, for instance
with del parrot.)
or the creation of an __all__ containing an exclusive list of names for
export.

No.

__all__ only effects names imported with "from module import *", it has
no effect on "import module".

What was that again about avoiding "writing anything that will be
misconstrued by newless cloobs unfortunate enough to reach this thread as
a result of a search for meaningful information on Python imports"?
Well, precisely.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline so I couldn't cat it

Oct 5 '07 #39
In message <5m************ @mid.uni-berlin.de>, Marc 'BlackJack' Rintsch
wrote:
To me a `variable` is made of a name, a memory address, a data type, and
a value. In languages like C the address and type are attached to the
name while in Python both are attached to the value.
How does C++ with RTTI fit into your picture, then?
Oct 6 '07 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3164
by: Doug Farrell | last post by:
Hi all, I'm trying to do the following from within a code module: import re # text to match text = "Good morning x something /x, how are you today x something else /x"
2
1995
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for my wrapper. This is acually part of the project, libxmlconf on sourceforge. The newest working version isn't there yet, and cvs is lagged by 6 hours or so. So if you think you want to have a try at this I can tgz the source for you. My...
18
3053
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
17
2049
by: Jacob Page | last post by:
I have created what I think may be a useful Python module, but I'd like to share it with the Python community to get feedback, i.e. if it's Pythonic. If it's considered useful by Pythonistas, I'll see about hosting it on Sourceforge or something like that. Is this a good forum for exposing modules to the public, or is there somewhere more-acceptable? Does this newsgroup find attachments acceptable? -- Jacob
9
2869
by: BartlebyScrivener | last post by:
I know this must have been answered a hundred times, but I must be searching on the wrong terminology. Let's say I have a module foo.py that imports os. I make another script called bar.py that imports foo.py and now I want to use, say, os.walk in bar.py. Which is faster or more correct or whatever: Do I import os at the top of bar.py and use foo's functions?
33
56500
by: christophertidy | last post by:
Hi I am new to Python and have recieved this error message when trying to instantiate an object from a class from another file within the same directory and wondered what I have done wrong. I have a Step.py class: class Step(object) def __init__(self, sName): "Initialise a new Step instance"
4
1663
by: Peter J. Bismuti | last post by:
I'm having trouble understanding how namespaces work in modules. I want to execute a module within the interpreter and then have values that are calculated persist so that other modules that get executed can retrieve them. For example, consider the two simple modules below. The first method fails and I'm not sure exactly why. (Note: assume one instance of an interpreter. In my case a 3rd party software tool that starts an interpreter...
3
1465
by: Jugdish | last post by:
Why doesn't the following work? $HOME/pkg/__init__.py $HOME/pkg/subpkg/__init__.py $HOME/pkg/subpkg/a.py $HOME/pkg/subpkg/b.py # empty import a
2
1839
by: Joe Strout | last post by:
Some corrections, to highlight the depth of my confusion... On Nov 11, 2008, at 9:10 PM, Joe Strout wrote: Actually, it does not. And no, it isn't; it's the NAME of the module the function is in. I'm not sure what good that does me. docstring.testmod does take an
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.