P3k __builtins__ identifiers -> warning

Here is a nice little Python 3 program, test.py: import string buffer = string.ascii_letters bytes = [] sum = 0 for chr in buffer: int = ord(chr) if 32 <= int < 127: bytes.append(chr) sum += 1 str = "".join(bytes) print(sum, str) If run as: python30a -W all test.py It produces the expected output: 52 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ But unfortunately it uses as identifiers: buffer, bytes, chr, int, sum, and str. None of these are keywords so none of them provokes a SyntaxError. In fact there are over 130 such identifiers; print(dir(__builtins__)) to see them. I think many newcomers to Python will find it difficult to remember 160 identifiers (keywords + __builtins__) and since some of them have appealing names (esp. buffer, bytes, min, max, and sum), they may make use of them without realising that this could cause them problems later on. My python-idea is that if python is run with -W all then it should report uses of __builtins__ as identifiers. -- Mark Summerfield, Qtrac Ltd., www.qtrac.eu

IMO this is a task for tools llike pylint or pychecker (both of which flag this). Also, it's controversial -- especially since you're unlikely to want to use a builtin whose name you can't remember. :-) The builtins were not made keywords for a reason. --Guido On Nov 27, 2007 12:31 AM, Mark Summerfield <mark@qtrac.eu> wrote:
-- --Guido van Rossum (home page: http://www.python.org/~guido/)

Mark Summerfield wrote:
My python-idea is that if python is run with -W all then it should report uses of __builtins__ as identifiers.
This could never work as the stdlib violates this rule and would invoke a large number of these warnings. And given the controversy about it, I doubt anyone is that interested in patching the stdlib to avoid these names. As far as I am concerned, I don't really see the point in avoiding these names. As you say, several of them are very attractive and just because there is a built-in with that name doesn't always deter me from using it. The scoping rules of python are fairly simple, so it is not difficult to keep track of the shadowing. And it's pretty easy to recover a built-in by retrieving the object from the __builtins__ module, though not very obvious to newcomers. -Scott -- Scott Dial scott@scottdial.com scodial@cs.indiana.edu

IMO this is a task for tools llike pylint or pychecker (both of which flag this). Also, it's controversial -- especially since you're unlikely to want to use a builtin whose name you can't remember. :-) The builtins were not made keywords for a reason. --Guido On Nov 27, 2007 12:31 AM, Mark Summerfield <mark@qtrac.eu> wrote:
-- --Guido van Rossum (home page: http://www.python.org/~guido/)

Mark Summerfield wrote:
My python-idea is that if python is run with -W all then it should report uses of __builtins__ as identifiers.
This could never work as the stdlib violates this rule and would invoke a large number of these warnings. And given the controversy about it, I doubt anyone is that interested in patching the stdlib to avoid these names. As far as I am concerned, I don't really see the point in avoiding these names. As you say, several of them are very attractive and just because there is a built-in with that name doesn't always deter me from using it. The scoping rules of python are fairly simple, so it is not difficult to keep track of the shadowing. And it's pretty easy to recover a built-in by retrieving the object from the __builtins__ module, though not very obvious to newcomers. -Scott -- Scott Dial scott@scottdial.com scodial@cs.indiana.edu
participants (3)
-
Guido van Rossum
-
Mark Summerfield
-
Scott Dial