__builtin__ quote

S

Steven Woody

Hi,

I am a new leaner and I get a question: abs() is a member of
__builtin__ module, but why should I use abs() rather than
__builtin__.abs() ? Thanks.
 
J

James Stroud

Steven said:
Hi,

I am a new leaner and I get a question: abs() is a member of
__builtin__ module, but why should I use abs() rather than
__builtin__.abs() ? Thanks.

It saves typing.

This might help your understanding:

py> import __builtin__
py> __builtin__.abs is abs
True


James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
 
S

Steven Woody

It saves typing.

This might help your understanding:

py> import __builtin__
py> __builtin__.abs is abs
True

Does that mean someone did 'import * from __builtin__' when python startup?
 
C

Chris Rebert

Does that mean someone did 'import * from __builtin__' when python startup?

In a sense, yes. The interpreter effectively automatically does such an import.

Cheers,
Chris
 
J

James Stroud

Steven said:
Does that mean someone did 'import * from __builtin__' when python startup?

In terms of the exact implementation of the cPython interpreter, I don't
know. But the interpreter behaves as if someone did just that. So there
is nothing wrong with thinking of it this way if it helps you understand
the interpreter.

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
 
J

John Machin

Hi,

I am a new leaner and I get a question:  abs() is a member of
__builtin__ module, but why should I use abs() rather than
__builtin__.abs() ? Thanks.

Fewer keystrokes.
 
G

Gabriel Genellina

In terms of the exact implementation of the cPython interpreter, I don't
know. But the interpreter behaves as if someone did just that. So there
is nothing wrong with thinking of it this way if it helps you understand
the interpreter.

Not exactly. Built-in names are "one step further" global names; it's not
that builtin names populate by default the global namespace. As local
names "hide" (or "shadow") global names, those global names "hide" builtin
names. Those three namespaces are distinct, like onion layers (global and
local namespaces are the same at the module level).

If the interpreter did the equivalent of "from __builtin__ import *",
redefining builtin names would destroy them, but that's not the case:

# the builtin "abs" function<built-in function abs>

# this one "hides" the builtin abs.... return "Hi, I'm the abs() global function!"
....<function abs at 0x00B9F2F0>

# remove the global "abs"
# we can access again the builtin "abs"<built-in function abs>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top