Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'widely': 0.05; 'attribute': 0.07; 'variables': 0.07; 'string': 0.09; '"if': 0.09; '__name__': 0.09; 'method,': 0.09; 'namespace': 0.09; 'subject:Why': 0.09; 'type,': 0.09; 'python': 0.11; 'stored': 0.12; '1:13': 0.16; 'attribute.': 0.16; 'callable': 0.16; 'collections': 0.16; 'declared': 0.16; 'imported.': 0.16; 'itself,': 0.16; 'merely': 0.16; 'namedtuple': 0.16; "object's": 0.16; 'objects.': 0.16; 'wrote:': 0.18; 'module': 0.19; 'seems': 0.21; 'example': 0.22; 'import': 0.22; 'questions:': 0.24; 'mon,': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'feature': 0.29; 'raise': 0.29; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'asked': 0.31; 'went': 0.31; 'code': 0.31; 'getting': 0.31; 'commonly': 0.31; 'restricted': 0.31; 'skip:= 40': 0.31; 'types.': 0.31; 'class': 0.32; 'probably': 0.32; 'run': 0.32; 'url:python': 0.33; 'implemented': 0.33; 'minimal': 0.33; 'not.': 0.33; 'display': 0.35; 'classes': 0.35; 'common': 0.35; 'created': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'accessible': 0.36; 'instances': 0.36; 'leads': 0.36; 'doing': 0.36; "didn't": 0.36; 'useful': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'days': 0.60; 'even': 0.60; 'read': 0.60; 'expression': 0.60; 'referred': 0.60; 'ago,': 0.61; 'john': 0.61; 'information': 0.63; 'name': 0.63; 'more': 0.64; 'kept': 0.65; 'determine': 0.67; 'frequently': 0.68; 'subject:get': 0.81; 'discover': 0.82; 'shadow': 0.84; 'url:lang': 0.84; '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:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=Uf8vTjgqLdptFxYiZ97SstopP6/0AsNhNhr+yOaEtgo=; b=H4/OdxmlYbYHJ6PrR6PpA95Ij9Na+APQvLoYZ+Z6s5D2pEF1MwQAQpPabb1hOBX3YE 2aFuduFhu3Txe+39huENkZZ/Ef62FaHJnCRkUiiUv8HDeBUhxti6CFOBiLp4s9WJJVGJ m7kypXDBc5cqqv0m+nwLgIaUarlYQK/BvjcF9fdMKMgBslnWVeFEt+C2DoG5Kai/eR+V FLPSxLnNQ0Jv2qNYigQP5kP4och8MmADzCNyq9hhPYNL+8Uzu1qGodDtya7y+NRgRbns UdpXHbZgKQkTWgXHKgfkFZfFoUMq6wwrF/lfieV9GYkRSeoSYeCQ08pv4v5WNOkyDhlI Xxxg== X-Received: by 10.66.188.172 with SMTP id gb12mr4275340pac.143.1384807448225; Mon, 18 Nov 2013 12:44:08 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Mon, 18 Nov 2013 13:43:28 -0700 Subject: Re: Why do only callable objects get a __name__? To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384807451 news.xs4all.nl 15873 [2001:888:2000:d::a6]:46007 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59906 On Mon, Nov 18, 2013 at 1:13 PM, John Ladasky wrote: > A few days ago, I asked about getting the original declared name of a fun= ction or method, and learned about the __name__ attribute. > > https://groups.google.com/forum/#!topic/comp.lang.python/bHvcuXgvdfA > > Of course, I have used __name__ for years in the common expression "if __= name__ =3D=3D "__main__") to determine whether a particular module is being= run or merely imported. But until recently, I never went deeper than that= . > > I just created an object using collections.namedtuple, and was surprised = to discover that it didn't have a __name__ -- even though something that be= haves like __name__ is clearly accessible and printable. Here's some minim= al Python 3.3.2 code and output: > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > from collections import namedtuple > > MyNamedTupleClass =3D namedtuple("ANamedTuple", ("foo", "bar")) > nt =3D MyNamedTupleClass(1,2) > print(nt) > print(type(nt)) > # print(nt.__name__) # this would raise an AttributeError > print(type(nt).__name__) # this is the desired output > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > ANamedTuple(foo=3D1, bar=3D2) > > ANamedTuple > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > As you can see, I snooped around in the object's type. I found that the = type, rather than the object itself, had the __name__ I was seeking. I the= n read the Python docs concerning __name__ and found that this attribute is= restricted to callable objects. > > This leads me to ask two questions: > > 1. WHY do only callable objects get a __name__? A __name__ would seem to= be a useful feature for other types. Clearly, whoever implemented namedtu= ple thought it was useful to retain and display that information as a part = of the string representation of the namedtuple (and I agree). Classes and functions are frequently kept in module namespaces, where they are known by a specific name. The intent is that the __name__ attribute should match that name by which it is commonly referred. Specific instances are not typically widely referred to by set names in this way. They are more commonly stored in variables that are used to hold a wide variety of objects. In the namedtuple example that you give, it seems that you would want the names of all instances of the ANamedTuple class to be the same "ANamedTuple", and I really don't see what the purpose of giving them all the same name would be. > 2. If I created a superclass of namedtuple which exposed type(namedtuple)= .__name__ in the namespace of the namedtuple itself, would I be doing anyth= ing harmful? Probably not. But why not just invent your own name attribute rather than shadow the one that Python designates for classes and functions?