On 16 Dec 2013 02:58, "Ethan Furman" <et...@stoneleaf.us> wrote: > > On 12/14/2013 07:51 PM, Steven D'Aprano wrote: >> >> On Sun, Dec 15, 2013 at 11:25:10AM +1000, Nick Coghlan wrote: >> >>> Oh, yes, a %T shortcut for "length limited type name of the supplied >>> object" would be brilliant. We need this frequently for C level error >>> messages, and I almost always have to look at an existing example to >>> remember the exact incantation :) >> >> >> What are the chances that could be made available from pure Python too? >> Having to extract the name of the type is a very common need for error >> messages, and I never know whether I ought to write type(obj).__name__ >> or obj.__class__.__name__. A %T and/or {:T} format code could be the One >> Obvious Way to include the type name in strings > > > +1
It's less obviously correct for Python code, though. In C, we're almost always running off slots, so type(obj).__name__ has a very high chance of being what we want, and is also preferred for speed reasons (since it's just a couple of pointer dereferences). At the Python level, whether to display obj.__name__ (working with a class directly), type(obj).__name__ (working with the concrete type, ignoring any proxying) or obj.__class__.__name__ (which takes proxying into account) really depends on exactly what you're doing, and the speed differences between them aren't so stark. Cheers, Nick. > > -- > ~Ethan~ > > _______________________________________________ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: https://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com