[Python-ideas] Changing str(someclass) to return only the class name
Spectral One
ghostwriter402 at gmail.com
Fri Oct 21 22:57:55 CEST 2011
> If you want the name of the class, ask for its name, not its string
> representation. Generally speaking, the string representation should
> tell you what sort of object it is (either explicitly or implicitly),
> not just its value.
I agree with this rather generally. I'd also like access to the class
name, but I don't really like that implementation yet.
A thought:
Could this be reframed as a tweak to the reporting of type objects,
specifically?
If the __repr__ function of a type object returned the desired class
information, it seems like it would solve this issue.
Currently,
print str(type(variable)) # returns a string in the form of
#"'module reference and class name' type.__name__ at address"
print repr(type(variable)) # returns the same string.
print type(variable).__name__ # returns "instance" for any user
variable instances.
However, if we altered repr to return the qualified name, we'd be pretty
much set, as we already have the __name__ variable in type objects* and
the __str__ can remain untouched. That would make the information rather
easily accessed and without special parsing.
(*I'd prefer a short statement to extract that name, too. I could work
with "print class(variable)" if the parser could. Other options off the
top o' my noggin: "type.name(variable)" or "variable.__name__" where the
.__name_ is automatically supplied. (allow overwriting in the class
definition?))
-Nate
More information about the Python-ideas
mailing list