[Python-Dev] Rationale for NamedTemporaryFile?

Georg Brandl g.brandl at gmx.net
Wed Mar 21 14:51:42 CET 2007


Aaron Bingham schrieb:
> Georg Brandl wrote:
>> Greg Ewing schrieb:
>>   
>>> Scott Dial wrote:
>>>     
>>> A tangential question -- why are TemporaryFile and
>>> NamedTemporaryFile named in TitleCase, when they're
>>> functions and not classes?
>>>     
>>
>> Probably because they are factory functions potentially returning a
>> _TemporaryFileWrapper. If they were "functions", they'd have to be
>> verbed, e.g. "make_temporary_file()".
>>
>> The class/function distinction is not so clear in Python from the user's
>> point of view since there is no different calling syntax.
> Actually the distinction is very clear:
> 
>  >>> class _PrivateClass(object):
> ...     pass
> ...
>  >>> def FunctionNamedToLookLikeClass():
> ...     return _PrivateClass()
> ...
>  >>> pc = _PrivateClass()
>  >>> isinstance(pc, _PrivateClass)
> True
>  >>> fntllc = FunctionNamedToLookLikeClass()
>  >>> isinstance(fntllc, FunctionNamedToLookLikeClass)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: isinstance() arg 2 must be a class, type, or tuple of classes 
> and types
> 
> That's sure to be an unpleasant surprise for someone.

<whisper>duck typing...</whisper>

But you have a valid point.

Georg



More information about the Python-Dev mailing list