Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'win32': 0.03; 'heavily': 0.04; 'syntax': 0.04; 'interpreter': 0.05; 'root': 0.05; 'assignment': 0.07; 'builtin': 0.09; 'forcing': 0.09; 'function:': 0.09; 'global,': 0.09; 'namespace': 0.09; 'objects,': 0.09; 'seemed': 0.09; 'whatever.': 0.09; 'contribute': 0.11; 'python': 0.11; 'def': 0.12; 'ah!': 0.16; 'builtins': 0.16; 'builtins.': 0.16; 'dict': 0.16; 'foo():': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'identifiers': 0.16; 'janssen': 0.16; 'keyword,': 0.16; 'keyword.': 0.16; 'name",': 0.16; 'namespace,': 0.16; 'qualified.': 0.16; 'readability': 0.16; 'syntaxerror:': 0.16; 'then?': 0.16; 'tuple,': 0.16; 'world!': 0.16; 'world!")': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'variable': 0.18; 'bit': 0.19; "python's": 0.19; 'thu,': 0.19; '>>>': 0.22; '(in': 0.22; 'separate': 0.22; 'print': 0.22; 'string,': 0.24; 'first,': 0.26; 'right.': 0.26; 'pass': 0.26; 'skip:_ 20': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'included': 0.31; 'code': 0.31; 'getting': 0.31; '"",': 0.31; '13,': 0.31; 'int,': 0.31; 'names.': 0.31; 'sep': 0.31; 'file': 0.32; 'themselves': 0.32; 'level.': 0.33; 'not.': 0.33; 'received:209.85': 0.35; 'classes': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'etc.)': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; 'raising': 0.36; 'should': 0.36; 'received:209': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'that,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'remove': 0.60; 'lost': 0.61; 'simply': 0.61; "you're": 0.61; 'name': 0.63; 'such': 0.63; 'more': 0.64; 'different': 0.65; 'invalid': 0.68; 'special': 0.74; '"".': 0.84; 'pirates': 0.84; 'absolutely': 0.87; '2013,': 0.91; 'magical': 0.91; 'subject:Don': 0.91; 'imagine': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=v8tCplU7UPytTI02Kfm9z209ZJiafHKX0CgW1gnBRDc=; b=EbecGIXRj1lRIs2SWtzYYVPOoRhsvMfRIoRgR9jtlPGNL4PnLzmnvf/aytH9Lse57e eVQCpDzn7G6MRq9E674pRqZjMefIo/AKGjRHLoQkBQ3b6qQ0Lv27SF6ncZ0GPc/3dE13 NDotBFK0Q3SgQUYKk6H3oabrUwTzA6ES+pr+92LiyqmBZ0CT5OznCYTjc/bT3FvHeH3y ZsCVnS/L29I9bFzWTlhhfftrvM1MI87v916f3cKi2GN/j1pV+lB3I++epjml0WaV0elC J6RyqRbiuxpZFJsRBpl9GHENFkNUHkR7QB1Vi11j5uk7LK4xmt/M6OuILPcEjd3s0+6/ 9HWQ== MIME-Version: 1.0 X-Received: by 10.52.117.16 with SMTP id ka16mr8797135vdb.43.1371081206989; Wed, 12 Jun 2013 16:53:26 -0700 (PDT) In-Reply-To: References: Date: Thu, 13 Jun 2013 09:53:26 +1000 Subject: Re: "Don't rebind built-in names*" - it confuses readers From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371081214 news.xs4all.nl 15943 [2001:888:2000:d::a6]:48589 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47861 On Thu, Jun 13, 2013 at 9:07 AM, Mark Janssen wrote: >>> You're right. I was being sloppy. > Okay, now I'm a bit confused. "print" is both a and a > member of the builtins. What happens then? Ah! I see where we are getting confused. When you said keyword, did you mean keyword, a person who has lost his parents... oops, that's Pirates of Penzance. Ahem. In Python 2.x, 'print' is actually a keyword. It has its own special syntax (eg printing to something other than stdout), and absolutely cannot be overridden: Python 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print=1 File "", line 1 print=1 ^ SyntaxError: invalid syntax But in Python 3, it's a builtin function: Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> foo=print >>> print=1 >>> foo("Hello, world!") Hello, world! > And abs(), max(), hex() and such seemed like keywords to my > scientific self (due to never having to "include"/import them), but > clearly their not. The builtins don't need to be imported, but they're identifiers like anything else. They're a namespace that gets searched after module-globals. int = 1 def foo(): int = 2 print(int) This has double shadowing :) It'll print 2, because locals get searched first, but if you remove that assignment then it'll print 1, and if you remove _that_ one, then it'll print "". There's nothing magical about the name int, but before raising NameError, the interpreter will look for it in builtins. You can even add more "builtins", though I would *strongly* advise against this unless you have a really REALLY good reason: >>> __builtins__.helloworld=123 >>> helloworld 123 >>> helloworld=234 >>> helloworld 234 >>> del helloworld >>> helloworld 123 > And int, list, tuple, dict and such always seemed > like keywords to my CS self because they were included in Python's > type system (like "int" would be in C). Yep, but in Python, types/classes are themselves objects, so you can pass them around like anything else. This also downgrades them from "language keyword" to "always-available name", which in effect upgrades your _own_ classes to the same level. > They are all one-step removed from keywords. And yet, since they are > not in a separate namespace, they should not be used as variable > names. Perhaps since they are very different from one another, they > should be put in separate namespaces off of a global, root > namespace... (math, string, etc.) There's no point forcing them to be looked up in a two-step process. If you want that, you can simply reference them as __builtins__.whatever, but you can instead just reference them as the unadorned name whatever. They contribute heavily to the simplicity and readability of Python code - imagine if every call to len() had to be qualified. ChrisA