473,785 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proposal: add sys to __builtins__

What would people think about adding sys to __builtins__ so that
"import sys" is no longer necessary? This is something I must add to
every script I write that's not a one-liner since they have this idiom
at the bottom:

if __name__ == "__main__":
sys.exit(main(s ys.argv[1:]))

Additionally, the necessity of "import sys" makes some one-liners a
little more unwieldy than they should be--it is surely the module I am
missing the most in one-liners. For example, with this proposal, this
inelegant one-liner:

$ python -c "import sys; print ''.join(sorted( sys.stdin.readl ines()))"

could be replaced by:

$ python -c "print ''.join(sorted( sys.stdin.readl ines()))"

Since sys is surely the most commonly used module (it is imported in 108
of 188 Python 2.4 stdlib modules on my system, and certainly more than
any other module), I would hope few people would be affected by a
namespace collision.

Other languages (e.g. C#) always make their system namespace available
without needing a special import.

In short, given the wide use of sys, its unambiguous nature, and the
fact that it really is built-in already, although not exposed as such, I
think we would be better off if sys were always allowed even without an
import statement.
--
Michael Hoffman
Sep 1 '05 #1
21 1531
Michael Hoffman <ca*******@mh39 1.invalid> wrote in
news:df******** **@gemini.csx.c am.ac.uk:
What would people think about adding sys to __builtins__ so that
"import sys" is no longer necessary? This is something I must
add to every script I write that's not a one-liner since they
have this idiom at the bottom:

if __name__ == "__main__":
sys.exit(main(s ys.argv[1:]))

Additionally, the necessity of "import sys" makes some
one-liners a little more unwieldy than they should be--it is
surely the module I am missing the most in one-liners. For
example, with this proposal, this inelegant one-liner:

$ python -c "import sys; print
''.join(sorted( sys.stdin.readl ines()))"

could be replaced by:

$ python -c "print ''.join(sorted( sys.stdin.readl ines()))"

Since sys is surely the most commonly used module (it is
imported in 108 of 188 Python 2.4 stdlib modules on my system,
and certainly more than any other module), I would hope few
people would be affected by a namespace collision.

Other languages (e.g. C#) always make their system namespace
available without needing a special import.

In short, given the wide use of sys, its unambiguous nature, and
the fact that it really is built-in already, although not
exposed as such, I think we would be better off if sys were
always allowed even without an import statement.


+1 here. As far as I'm concerned, both os and sys could be special-
cased that way. That said, I would guess the likelihood of that
happening is 0.

--
rzed
Sep 1 '05 #2
Rick Wotnaz wrote:
Michael Hoffman <ca*******@mh39 1.invalid> wrote in
news:df******** **@gemini.csx.c am.ac.uk:

What would people think about adding sys to __builtins__ so that
"import sys" is no longer necessary? This is something I must
add to every script I write that's not a one-liner since they
have this idiom at the bottom:

if __name__ == "__main__":
sys.exit(main(s ys.argv[1:]))

Additionall y, the necessity of "import sys" makes some
one-liners a little more unwieldy than they should be--it is
surely the module I am missing the most in one-liners. For
example, with this proposal, this inelegant one-liner:

$ python -c "import sys; print
''.join(sorte d(sys.stdin.rea dlines()))"

could be replaced by:

$ python -c "print ''.join(sorted( sys.stdin.readl ines()))"

Since sys is surely the most commonly used module (it is
imported in 108 of 188 Python 2.4 stdlib modules on my system,
and certainly more than any other module), I would hope few
people would be affected by a namespace collision.

Other languages (e.g. C#) always make their system namespace
available without needing a special import.

In short, given the wide use of sys, its unambiguous nature, and
the fact that it really is built-in already, although not
exposed as such, I think we would be better off if sys were
always allowed even without an import statement.

+1 here. As far as I'm concerned, both os and sys could be special-
cased that way. That said, I would guess the likelihood of that
happening is 0.

I wonder if it would be worth special-casing the AttributeError
exception handling at the outermost lexical scope to try and import a
module with the troublesome name and then retrying the attribute access
if the import succeeded (possibly even from a path limited to the
standard locations).

That way none of the standard library modules would need to be imported
before use.

I can see that this would create problems as well as solving some, but
it would be nice not to have to import the standard library modules. I
don't personally find it a hardship, but some people do.

though-i-could-just-be-raving-ly y'rs - steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 2 '05 #3
Steve Holden wrote:
I wonder if it would be worth special-casing the AttributeError [snip]


What is it that Tim Peters said? "Special cases aren't special
enough..."

Besides, a better way is to use your ~/.pythonrc file for customizing
according to your needs.

A simple:

echo "import sys, os" >> ~./pythonrc

will do the job.

Thanks,
John Benediktsson

Sep 2 '05 #4
MrJbQ7 wrote:
Steve Holden wrote:
I wonder if it would be worth special-casing the AttributeError [snip]
What is it that Tim Peters said? "Special cases aren't special
enough..."


That suggestion is a little too much magic for me.
Besides, a better way is to use your ~/.pythonrc file for customizing
according to your needs.

A simple:

echo "import sys, os" >> ~./pythonrc

will do the job.


Until someone else tries to use your script or module.
--
Michael Hoffman
Sep 2 '05 #5
Michael Hoffman a écrit :
MrJbQ7 wrote:
Besides, a better way is to use your ~/.pythonrc file for customizing
according to your needs.

A simple:

echo "import sys, os" >> ~./pythonrc

will do the job.


Until someone else tries to use your script or module.


A developper should not be too lazy to add one small line in a complete
script/module.
Besides your entire justification to this proposal was based on shell
one-liners, not script or modules.

Sep 2 '05 #6
tiissa wrote:
A developper should not be too lazy to add one small line in a complete
script/module.
To the contrary, I agree with Larry Wall that laziness is one of the
cardinal virtues of a programmer. Although my personal desire to be lazy
is not at issue here--I always start new scripts and modules from a
template that includes import sys.

Would you argue that the language is superior because half of its
modules must have "import sys" at the beginning, although sys is already
imported? The only difference is that the namespace is not exposed by
default. I suggest that the default be changed.

It would simplify introductions to the language as well. In the current
tutorial it is necessary to use "import sys" before it has time to
explain modules and importing.
Besides your entire justification to this proposal was based on shell
one-liners, not script or modules.


Sorry, that's incorrect; please go back and read the original post again.
--
Michael Hoffman
Sep 2 '05 #7
Steve Holden wrote:
Rick Wotnaz wrote:
Michael Hoffman <ca*******@mh39 1.invalid> wrote in
news:df******** **@gemini.csx.c am.ac.uk:
What would people think about adding sys to __builtins__ so that
"import sys" is no longer necessary? This is something I must
add to every script I write that's not a one-liner since they
have this idiom at the bottom:

if __name__ == "__main__":
sys.exit(main(s ys.argv[1:]))

Additionally, the necessity of "import sys" makes some
one-liners a little more unwieldy than they should be--it is
surely the module I am missing the most in one-liners. For
example, with this proposal, this inelegant one-liner:

$ python -c "import sys; print
''.join(sorted( sys.stdin.readl ines()))"
could be replaced by:

$ python -c "print ''.join(sorted( sys.stdin.readl ines()))"

Since sys is surely the most commonly used module (it is
imported in 108 of 188 Python 2.4 stdlib modules on my system,
and certainly more than any other module), I would hope few
people would be affected by a namespace collision.

Other languages (e.g. C#) always make their system namespace
available without needing a special import.

In short, given the wide use of sys, its unambiguous nature, and
the fact that it really is built-in already, although not
exposed as such, I think we would be better off if sys were
always allowed even without an import statement.


+1 here. As far as I'm concerned, both os and sys could be special-
cased that way. That said, I would guess the likelihood of that
happening is 0.


I wonder if it would be worth special-casing the AttributeError
exception handling at the outermost lexical scope to try and import a
module with the troublesome name and then retrying the attribute access
if the import succeeded (possibly even from a path limited to the
standard locations).

That way none of the standard library modules would need to be imported
before use.

I can see that this would create problems as well as solving some, but
it would be nice not to have to import the standard library modules. I
don't personally find it a hardship, but some people do.

though-i-could-just-be-raving-ly y'rs - steve


This sounds pretty interesting. How about a switch to invoke this
handling for the one-liner crowd and those who wish to use it?

Somehow, I never heard any C programmers suggest that the default
processing not include the need for:

#include <stdio.h>
Sep 2 '05 #8
Michael Hoffman wrote:
To the contrary, I agree with Larry Wall that laziness is one of the
cardinal virtues of a programmer.
There's lazy and too lazy.
You don't want to be too lazy to even get out of bed to code in Python.
Of course, with Perl, that's entirely another mattress^Wmatte r.

Would you argue that the language is superior because half of its
modules must have "import sys" at the beginning
I wouldn't dare arguing about superiority.

I was just stating your proposal didn't really solve anything. A good
editor/template and .pythonrc already save you the typing of 'import
sys' in scripts for the former and shell command for the latter.

Sorry, that's incorrect


Alright, that was a bit of an overstatement.
I should have said your proposal is perceptibly useful in those shell
one-liners. The distribution of script and modules is another matter.
As for my opinion, you've already guessed I don't perceive 'import sys'
as an issue. Therefore, the special case of an implicit import of sys
does not appeal to me.
Sep 3 '05 #9
Paul Watson wrote:
This sounds pretty interesting. How about a switch to invoke this
handling for the one-liner crowd and those who wish to use it?
Somehow, I never heard any C programmers suggest that the default
processing not include the need for:

#include <stdio.h>

I think it is because that we cannot modify the C language, but
python is a language that's still evolving.
Sep 4 '05 #10

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

Similar topics

1
1808
by: Opinder | last post by:
Hi, For the Python experts out there: Are there any side effects in assigning new variables to the __builtins__ module for the purpose of exposing variables to imported modules. For example.
0
1762
by: Mathieu Fenniak | last post by:
Good day, I'm writing an application which creates modules and runs Python code on the fly, embedding Python. The main function of the code is loaded into the __main__ module: (Error checking omitted for brevity.) PyObject* module = PyImport_AddModule("__main__"); PyObject* moduleDict = PyModule_GetDict(module); PyObject* runRetval = PyRun_String(codeStr, Py_file_input, moduleDict, moduleDict);
1
1370
by: Michael Hohn | last post by:
Hi, using the file builtin_check.py with content # Module builtin_check # Inconstency in the binding of __builtins__ def get_binding(name): return locals() def get_global_binding(name):
4
1492
by: Collin Winter | last post by:
Hallo all, As it currently stands, the type of the global __builtins__ differs depending on whether you're in the __main__ namespace (__builtins__ is a module) or not (its a dict). I was recently tripped up by this discrepancy, and googling the issue brings up half-a-dozen or so c.l.p threads where others have been bitten by this, too. I'd like to propose that in Py3.0 (if not earlier), __builtins__ will be the same type regardless of...
1
1743
by: Adam Hupp | last post by:
I've noticed some unexpected behavior with __builtins__ during module import. It seems that during module import __builtins__ is a dict but at all other times it is a module. For example, if the file testmod.py has these contents: print type(__builtins__) print "has str attr", hasattr(__builtins__, 'str') The output differs depending on how it is run:
3
4176
by: loquehumaine | last post by:
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)
0
1086
by: Patrick Maupin | last post by:
__builtins__ in 2.5.2 doesn't seem to behave like I remember it did the last time I did some custom stuff with it, a very long time ago. This isn't surprising, because of ongoing optimization, but it's hard to google for '__builtins__' so I didn't really find any documentation on the current CPython behavior, which in some cases seems quite strange to me. The documentation node at http://docs.python.org/ref/naming.html has a "here be...
3
1373
by: Gabriel Genellina | last post by:
En Sun, 07 Sep 2008 14:00:48 -0300, Patrick Maupin <pmaupin@gmail.comescribió: Python takes some shortcuts when dealing with builtins. I'll just describe what happens (I won't say whether it is "right" or "wrong"). The exec statement, when given a string source, compiles it and eventually calls PyEval_EvalCodeEx, which creates a new frame using PyFrame_New and finally executes it. A frame object contains a pointer to the previous frame,...
0
1218
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote: when you pass mydict, it is used as the global variables in the eval, right? Then, you passed a code to eval('...', mydict), sometimes you might create global variable inside eval, and you want to see the value of that inner global, that's why python modified mydict as a side-effect. Then what is __builtins__ doing there? Because when eval created an environment for the code...
0
9645
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
9480
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
10147
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
10090
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,...
1
7499
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
6739
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
5380
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
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.