473,786 Members | 2,344 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 3493
En Wed, 03 Oct 2007 07:12:17 -0300, Lawrence D'Oliveiro
<ld*@geek-central.gen.new _zealandescribi �:
In message <ma************ *************** ***********@pyt hon.org>, Robert
Kern wrote:
>Lawrence D'Oliveiro wrote:
>>In message <ma************ *************** ***********@pyt hon.org>,
Robert
Kern wrote:

Not all of the modules in a package are imported by importing the
top-level package.

You can't import packages, only modules.

os.path is a particularly weird case because it is just an alias to
the
platform-specific path-handling module; os is not a package.

os is a module, os.path is a variable within that module. That's all
there is to it.

Yes, but os.path is also module. That's why I said it was a weird case.

You can't have modules within modules. os.path isn't an exception--see
below.
>In [1]: import os

In [2]: type(os.path)
Out[2]: <type 'module'>

On my Gentoo system:
>>import os
>>os.path
<module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>

It's just a variable that happens to point to the posixpath module.
A "module" is a certain type of Python objects, like ints, functions,
exceptions and all else.
The easiest way to create a module is to load it from file, but some
modules are already built into the interpreter, and you can even create a
module from scratch.

pyimport os
pytype(os)
<type 'module'>
pytype(os.path)
<type 'module'>
pyos
<module 'os' from 'c:\apps\python 25\lib\os.pyc'>
pyimport sys
pytype(sys)
<type 'module'>
pysys
<module 'sys' (built-in)>
pyModuleType = type(os)
pynewmodule = ModuleType('new module')
pynewmodule.a = 3
pynewmodule
<module 'newmodule' (built-in)>
pytype(newmodul e) is type(os.path)
True

"os" is a name that refers to the os module object. "os.path" means "the
path attribute in the object referenced by the name os", that happens to
be another module too.
os is not a package; os.path is set when the os module is imported,
depending on the platform. It may be ntpath, posixpath, macpath, or
whatever. On Windows:

pyimport ntpath
pyos.path is ntpath
True
pyimport macpath
pyimport posixpath
pymacpath.sep
':'
pyntpath.sep
'\\'

Apart from that, there is no magic involved, just plain attribute access
like everywhere else.

--
Gabriel Genellina

Oct 3 '07 #11
Lawrence D'Oliveiro wrote:
In message <ma************ *************** ***********@pyt hon.org>, Robert
Kern wrote:
>Lawrence D'Oliveiro wrote:
>>In message <ma************ *************** ***********@pyt hon.org>, Robert
Kern wrote:

Not all of the modules in a package are imported by importing the
top-level package.
You can't import packages, only modules.

os.path is a particularly weird case because it is just an alias to the
platform-specific path-handling module; os is not a package.
os is a module, os.path is a variable within that module. That's all
there is to it.
Yes, but os.path is also module. That's why I said it was a weird case.

You can't have modules within modules. os.path isn't an exception--see
below.
>In [1]: import os

In [2]: type(os.path)
Out[2]: <type 'module'>

On my Gentoo system:
>>import os
>>os.path
<module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>

It's just a variable that happens to point to the posixpath module.
I believe that is precisely what I was saying.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 3 '07 #12
+1 Subject line of the week (SLOTW)

rjcarr wrote:
So my question is ... why are they [os.path and logging.handler s] different?
[A] wrote:
Because you misspelled it. First, do a dir() on logging:
[b] wrote:
No, he didn't... OP: logging is a package and logging.handler s is one module
in the package... os.path is a particularly weird case...
[C] wrote:
You can't import packages, only modules.
[D] wrote:
Oh come on, this is unnecessary nitpicking.
[E] wrote:
You *can* import a package, and a package is just a *little* different from a
module
[b] wrote:
Yes, but os.path is also module. That's why I said it was a weird case.
[C] wrote:
You can't have modules within modules. os.path isn't an exception...It' s just
a variable that happens to point to the posixpath module.
[F] wrote:
There's no "pointing" going on. It's another name bound to the same object,
of equal status to the 'posixpath' name.
[G] wrote:
a module is just an object, that can of course be an attribute of another
module object
[H] wrote:
os is not a package; os.path is set when the os module is imported
[C] wrote:
You're right. I was misremembering the behaviour of PyCrypto
[J] wrote:
In Matlab you can use function dec2bin, hex2dec, dec2hex bin2dec functions to
convert decimal to binary and heximal etc.
[b] wrote:
I believe that is precisely what I was saying.

with apologies to all concerned :-)

Michael

Oct 3 '07 #13
In message <87************ @benfinney.id.a u>, Ben Finney wrote:
Lawrence D'Oliveiro <ld*@geek-central.gen.new _zealandwrites:
>On my Gentoo system:
> >>import os
>>os.path
<module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>

It's just a variable that happens to point to the posixpath module.

There's no "pointing" going on. It's another name bound to the same
object, of equal status to the 'posixpath' name.

Python doesn't have pointers, and even "variable" is a misleading term
in Python. Best to stick to "name" and "bound to".
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.

Honestly, why do people react to the word "pointer" as though computers have
to wear underwear to conceal something shameful going on in their nether
regions?
Oct 3 '07 #14
On Thu, 2007-10-04 at 11:11 +1300, Lawrence D'Oliveiro wrote:
In Python, all names _are_ variables. They are not "bound" to objects. The
value of os.path is a pointer.
No. "os.path" refers to the object that's known as the "path" attribute
of the object known as "os". That object, in turn, is a module.
It's implemented as a pointer,
While it is true that namespaces are implemented in CPython as
collections of pointers to PyObject structures, that's an irrelevant
implementation detail. I doubt that they are implemented as pointers in
Jython, PyPy, or IronPython.
it has all the semantics of a pointer.
No, it doesn't. A pointer means the physical address of a memory
location, which implies that you can overwrite that memory location. Can
you do that in Python?
Honestly, why do people react to the word "pointer" as though computers have
to wear underwear to conceal something shameful going on in their nether
regions?
I won't speak for "people", but maybe it's because Python acts precisely
as this underwear that does conceal the low-level regions of memory
management and bit-twiddling that Python programmers like to avoid in
favor of solving higher-level problems.

If it helps you to think of Python names as "kind of like pointers,"
you're free to do so, but it's only a weak analogy that can lead
beginners to drawing incorrect conclusions.

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 4 '07 #15
Lawrence D'Oliveiro wrote:
In message <87************ @benfinney.id.a u>, Ben Finney wrote:
>Lawrence D'Oliveiro <ld*@geek-central.gen.new _zealandwrites:
>>On my Gentoo system:

>>import os
>>os.path
<module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>

It's just a variable that happens to point to the posixpath module.
There's no "pointing" going on. It's another name bound to the same
object, of equal status to the 'posixpath' name.

Python doesn't have pointers, and even "variable" is a misleading term
in Python. Best to stick to "name" and "bound to".

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.

Honestly, why do people react to the word "pointer" as though computers have
to wear underwear to conceal something shameful going on in their nether
regions?
Because they have been told by their church that all God-fearing names
do what names have always done in programming languages, which is to
describe areas of memory of a particular size, type and locality.

You and I know that the semantics of Python names are precisely those of
(to use an Algol 68 term, unless I am mistaken) automatically
dereferenced pointers to objects of arbitrary type. I actually think
that's one of the neatest things about Python, and I believe it's no
accident that both Tim Peters and I were Icon enthusiasts.

But the rest of the world clings to its illusions.

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

Oct 4 '07 #16
Lawrence D'Oliveiro wrote:
In message <87************ @benfinney.id.a u>, Ben Finney wrote:
>Lawrence D'Oliveiro <ld*@geek-central.gen.new _zealandwrites:
>>On my Gentoo system:

>>import os
>>os.path
<module 'posixpath' from '/usr/lib64/python2.5/posixpath.pyc'>

It's just a variable that happens to point to the posixpath module.
There's no "pointing" going on. It's another name bound to the same
object, of equal status to the 'posixpath' name.

Python doesn't have pointers, and even "variable" is a misleading term
in Python. Best to stick to "name" and "bound to".

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.

Honestly, why do people react to the word "pointer" as though computers have
to wear underwear to conceal something shameful going on in their nether
regions?
Because they have been told by their church that all God-fearing names
do what names have always done in programming languages, which is to
describe areas of memory of a particular size, type and locality.

You and I know that the semantics of Python names are precisely those of
(to use an Algol 68 term, unless I am mistaken) automatically
dereferenced pointers to objects of arbitrary type. I actually think
that's one of the neatest things about Python, and I believe it's no
accident that both Tim Peters and I were Icon enthusiasts.

But the rest of the world clings to its illusions.

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

Oct 4 '07 #17
Steve Holden <st***@holdenwe b.comwrites:
You and I know that the semantics of Python names are precisely
those of (to use an Algol 68 term, unless I am mistaken)
automatically dereferenced pointers to objects of arbitrary type.
Yes. That's exactly why it's wrong to refer to them as pointers. They
don't behave like the pointers in other languages, which are *not*
"automatica lly-dereferenced", nor "to objects of arbitrary type".

It's misleading to talk about a Python name as a "pointer" without
those qualifiers, because they're *not* implicit in programmers'
understanding of that term.

If you want to continually refer to them as
"automatica lly-dereferenced pointers to objects of arbitrary type", by
all means go ahead. That at least *does* fit the semantics. But the
simple term "pointer" does *not* describe them in the absence of those
necessary and non-default qualifiers, and is misleading.

--
\ "I wish there was a knob on the TV to turn up the intelligence. |
`\ There's a knob called 'brightness' but it doesn't work." -- |
_o__) Eugene P. Gallagher |
Ben Finney
Oct 4 '07 #18
Ben Finney wrote:
Steve Holden <st***@holdenwe b.comwrites:
>You and I know that the semantics of Python names are precisely
those of (to use an Algol 68 term, unless I am mistaken)
automaticall y dereferenced pointers to objects of arbitrary type.

Yes. That's exactly why it's wrong to refer to them as pointers. They
don't behave like the pointers in other languages, which are *not*
"automatica lly-dereferenced", nor "to objects of arbitrary type".

It's misleading to talk about a Python name as a "pointer" without
those qualifiers, because they're *not* implicit in programmers'
understanding of that term.

If you want to continually refer to them as
"automatica lly-dereferenced pointers to objects of arbitrary type", by
all means go ahead. That at least *does* fit the semantics. But the
simple term "pointer" does *not* describe them in the absence of those
necessary and non-default qualifiers, and is misleading.
In practice I am quite happy talking about "variables" as long as nobody
is picking nits, but I am afraid that in this thread the nits are not
only being picked, they are being boiled alive and eaten.

Consequently I feel obliged to reply in my own defense that if you
weren't intelligent enough to recognize that description as essentially
correct this conversation need have gone no further. I don't encourage
people to talk in terms of pointers most of the time, but I find that
such pedagogic devices are useful in discussing the essential
differences between mutable and immutable values.

Neither do I normally attempt to mislead (as an examination of my record
on this group should confirm), which is precisely why the qualifiers
*were* attached. So kindly do not tell me it's "wrong" to refer them as
pointers. Such phraseology can tend to be emotionally charged (for
instance when proponents have religious feelings about their beliefs).

Fortunately programming languages and operating systems have never been
religious issues for me. It's more like "This problem has a cross head,
so I'll need a Philips screwdriver". I don't worship my spirit level, so
I'm damned if I will worship LISP or Python. They can both be *jolly*
useful, though

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 4 '07 #19
In message <ma************ *************** ***********@pyt hon.org>, Carsten
Haese wrote:
On Thu, 2007-10-04 at 11:11 +1300, Lawrence D'Oliveiro wrote:
>In Python, all names _are_ variables. They are not "bound" to objects.
The value of os.path is a pointer.

No. "os.path" refers to the object that's known as the "path" attribute
of the object known as "os". That object, in turn, is a module.
No, it's a variable. It just happens to contain a pointer to a module.
> It's implemented as a pointer,

While it is true that namespaces are implemented in CPython as
collections of pointers to PyObject structures, that's an irrelevant
implementation detail. I doubt that they are implemented as pointers in
Jython, PyPy, or IronPython.
I'll bet they are.
> it has all the semantics of a pointer.

No, it doesn't. A pointer means the physical address of a memory
location, which implies that you can overwrite that memory location. Can
you do that in Python?
Yes. Look up the definition of "mutable objects".

Oct 4 '07 #20

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
1994
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
3052
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
2048
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
2867
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
56499
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
1661
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
1462
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
1837
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
9647
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
9491
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
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
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...
0
6744
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
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.