Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __builtins__

Reply
Thread Tools

__builtins__

 
 
loquehumaine@gmail.com
Guest
Posts: n/a
 
      02-08-2008
Hi there,
I'm a newby in python (I know a little in programmation) and I have
a lot of questions on builtins but my first one is about modules...

I have seen that if I type help() at a prompt, and then 'modules',
I'll be given a list of all modules available, thanks to this group..
But I have seen the differences between them and the one in
dir(__builtins__).
Why are some modules in __builtins__ and others don't ? (UserDict for
example)
Why dir(__builtins__) gives me "math" but not help(__builtins__) ?

What are the differences between __builtins__ and __builtin__ ? (By
the way, I have python 2.4)

Finally, if I do del(__builtins__), what can I do to repair the
"mistake" (as I got an import error __import__ not found if I want to
import __builtins__...?

That's may be obvious for you, but that's all strange to me and I
didn't find answers on the net...
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      02-08-2008
On Fri, 08 Feb 2008 00:25:14 -0800, loquehumaine wrote:

> I have seen that if I type help() at a prompt, and then 'modules',
> I'll be given a list of all modules available, thanks to this group..
> But I have seen the differences between them and the one in
> dir(__builtins__).
> Why are some modules in __builtins__ and others don't ? (UserDict for
> example)


`__builtins__` doesn't contain modules::

Python 2.4.4 (#2, Apr 12 2007, 21:03:11)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> inspect.getmembers(__builtins__, inspect.ismodule)

[]

> Why dir(__builtins__) gives me "math" but not help(__builtins__) ?


So there's no 'math' in `__builtins__`::

>>> 'math' in dir(__builtins__)

False

> What are the differences between __builtins__ and __builtin__ ? (By
> the way, I have python 2.4)


`__builtins__` is an implementation detail, and `__builtin__` is a name
of a module you can import. You should not use `__builtins__` but import
`__builtin__` and inspect that instead of `__builtins__`.

The (symmetric) difference of the two is empty::

>>> import __builtin__
>>> set(dir(__builtins__)).symmetric_difference(dir(__ builtin__))

set([])

> Finally, if I do del(__builtins__), what can I do to repair the
> "mistake" (as I got an import error __import__ not found if I want to
> import __builtins__...?


Don't ``del __builtins__`` in the first place.

> That's may be obvious for you, but that's all strange to me and I
> didn't find answers on the net...


So the real question is, why you see 'math' in `__builtins__`. It should
not be there.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
 
LHB
Guest
Posts: n/a
 
      02-08-2008
Marc 'BlackJack' Rintsch a écrit :
> On Fri, 08 Feb 2008 00:25:14 -0800, loquehumaine wrote:
>
>> I have seen that if I type help() at a prompt, and then 'modules',
>> I'll be given a list of all modules available, thanks to this group..
>> But I have seen the differences between them and the one in
>> dir(__builtins__).
>> Why are some modules in __builtins__ and others don't ? (UserDict for
>> example)

>
> `__builtins__` doesn't contain modules::

You are right... I don't know why I thought there was math here... It's
not in sys.modules either...
Is there "a place" where you can find a list of 'some' available modules
('standard' ones like math, sys, ...) but not all, or I really need a
break during the week-end?
If so, what the difference between the 'present' and the 'missing' ones?

For example, in http://docs.python.org/modindex.html for math: "This
module is always available." unlike pickle or HTMLParser. Is this only
because of the versions of Python?

I think I have mixed-up a lot of things and that I need a little bit
more of readings about builtin things... (Doc that goes further than
http://docs.python.org/lib/builtin.html)

> Python 2.4.4 (#2, Apr 12 2007, 21:03:11)
> [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import inspect
> >>> inspect.getmembers(__builtins__, inspect.ismodule)

> []

At least I have learn a new module =)


> `__builtins__` is an implementation detail, and `__builtin__` is a name
> of a module you can import. You should not use `__builtins__` but import
> `__builtin__` and inspect that instead of `__builtins__`.

Ok. Should I only see `__builtins__` as an access to builtin
functions/exception/... ?

> Don't ``del __builtins__`` in the first place.

Fair enough ^_^

> So the real question is, why you see 'math' in `__builtins__`. It should
> not be there.

I think the answer is that I need more rest...

>
> Ciao,
> Marc 'BlackJack' Rintsch

Thanks a lot,

LHB
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      02-09-2008
LHB <> writes:

> Marc 'BlackJack' Rintsch a écrit :
> > `__builtins__` is an implementation detail, and `__builtin__` is a name
> > of a module you can import. You should not use `__builtins__` but import
> > `__builtin__` and inspect that instead of `__builtins__`.

> Ok. Should I only see `__builtins__` as an access to builtin
> functions/exception/... ?


No, if you want that access, explicitly 'import __builtin__' and
access them that way. Ignore '__builtins__' altogether as an
implementation detail. (This is difficult to adhere to because the
names are confusingly similar; this is an acknowledged wart in current
Python.)

IIRC this behaviour will change in Python 3.0, where 'import
__builtin__' will be the *only* way to get at builtins from normal
code. At least, I'm now writing my code as though that's the case

--
\ “The man who is denied the opportunity of taking decisions of |
`\ importance begins to regard as important the decisions he is |
_o__) allowed to take.” —C. Northcote Parkinson |
Ben Finney
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: __builtins__ thread-safe / __builtins__ as function? Chris Angelico Python 0 10-14-2012 02:01 PM
Re: __builtins__ thread-safe / __builtins__ as function? Chris Angelico Python 0 10-11-2012 02:25 PM
A second __builtins__- or globals()-like namespace. Jacek Generowicz Python 1 04-08-2004 01:31 PM
"Virtual" file system mock object - replacing stuff in __builtins__ Remy Blank Python 3 03-06-2004 12:20 AM
Using the __builtins__ module to assign new global variables Opinder Python 1 03-02-2004 12:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57