Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:error': 0.03; 'interpreter': 0.05; 'attribute': 0.07; 'function,': 0.09; 'statements': 0.09; 'used.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'itself.': 0.14; 'any.': 0.16; 'callable': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func': 0.16; 'function"': 0.16; 'itself,': 0.16; 'objects.': 0.16; 'typeerror:': 0.16; "variable's": 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'of.': 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; '"",': 0.31; '25,': 0.31; 'asks': 0.31; 'name;': 0.31; 'file': 0.32; 'class': 0.32; 'stuff': 0.32; '(most': 0.33; 'cases': 0.33; "can't": 0.35; 'knows': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'in.': 0.36; 'object,': 0.36; 'useful': 0.36; 'recent': 0.39; 'explain': 0.39; 'called': 0.40; 'how': 0.40; 'identify': 0.61; 'matter': 0.61; 'show': 0.63; 'name': 0.63; 'such': 0.63; 'more': 0.64; 'nobody': 0.68; 'to,': 0.72; 'hand': 0.80; 'as:': 0.81; '"not': 0.84; "'class'": 0.84; "'foo'": 0.84; '2015': 0.84; 'one;': 0.84; 'to:none': 0.92; 'hands': 0.96 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:cc :content-type; bh=5mvnfdi6pe22iGcrCuh98T1PUrdCIAgNJ78gg0CE0rY=; b=nfgtsHfLs7pkdfdLDP0/UAPcx/FRxbtv/BKjsccff9Od8a9fPiJLaFKUp94QOSP2Yh rngZnI+lquP3Np4wH0H1OiBr9WlezHczgYMy34lKh9QvJHIMTuGNdfsmvwpGmY+tdrUi 8oYbfZEf3NaBqsEScZeFNMifsLfySTuKt2elYUKvyvGv4VGbcGy/xtTKaSOdDZtWEsSv +96CteNJsozdDAPvT1gqSQmjF+Kpa9ByT19nZqbizr2CAqeCz76rRR2esSY7iQZ8Q+6O vU54dr3MYjJR6DDhRbmBVY8WEBcARS0zlj0C+XTQF7yaoP1TK+iJGABoig8v2paSBKoc tg3A== MIME-Version: 1.0 X-Received: by 10.140.90.112 with SMTP id w103mr26252432qgd.65.1422138352010; Sat, 24 Jan 2015 14:25:52 -0800 (PST) In-Reply-To: References: <1a194e0a0b738d205de54180fa7@nntp.aioe.org> <54c39366$0$13006$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 25 Jan 2015 09:25:51 +1100 Subject: Re: __bases__ misleading error message From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422138354 news.xs4all.nl 2852 [2001:888:2000:d::a6]:55555 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84505 On Sun, Jan 25, 2015 at 9:09 AM, Mario Figueiredo wrote: > Meaning the interpreter knows a variable's name. Which would allow it to > produce an error message such as: > > AttributeError: 'foo' object has no attribute '__bases__' > > For the following code: > > class Sub: > pass > > foo = Sub() > foo.__bases__ Let me explain by way of analogy. You have ten shoeboxes to store your stuff in. I hand you a thing and say "Here, put this into shoebox #4". Then someone else comes along and says, "I need the thing from shoebox #4", so you give him that thing. Now, he hands that thing to someone else and asks him which shoebox it came out of, just by looking at the thing itself. How can he say? The thing doesn't have any way of knowing what shoebox it came out of. Python names reference objects. But once you get an object, there's no way of knowing which name was used to get to it. There might be one such name; there might be more than one; and there might not be any. You can't identify an object by the name it's bound to, but you can identify it by something that's always true of the object itself, like its type. There are a few cases where names are so useful that they get attached to the objects themselves. The 'def' and 'class' statements create objects and also record the names used. But you still can't identify what name was used to reference something: >>> def func(x): print("x = %s"%x) ... >>> func(123) x = 123 >>> show = func >>> show(234) x = 234 >>> func = "not a function" >>> func(123) Traceback (most recent call last): File "", line 1, in TypeError: 'str' object is not callable >>> show(123) x = 123 >>> show No matter what name I use to reference the function, it's still called "func". Nobody can ever know that I'm identifying it by the name 'show' now. ChrisA