[Python-ideas] Changing str(someclass) to return only the class name

Guido van Rossum guido at python.org
Sat Oct 22 06:35:41 CEST 2011


On Fri, Oct 21, 2011 at 9:25 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 10/21/2011 8:39 PM, Nick Coghlan wrote:
>
>>    str(module) ==>  module.__name__
>>    str(func)  ==>  func.__name__
>>    str(cls)  ==>  "{}.{}".format(cls.__module__, cls.__name__) # See note
>> below
>
> If you do this, then also do
> str(generator) ==> generator.__name__

Why? Assuming by "generator" you mean the iteror returned by calling a
generator function (not the generator function itself, which is
covered by str(func) ==> func.__name__), the generator has no "name"
which can be used to retrieve it. The three above (module, function,
class) all -- typically -- are referenced by variables whose name is
forced by the syntax (import foo, def foo, class foo). But that does
not apply to a generator.

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list