[Python-ideas] PEP 3156: getting the socket or peer name from the transport

Guido van Rossum guido at python.org
Fri Feb 1 02:58:32 CET 2013


On Thu, Jan 31, 2013 at 12:51 AM, Yuval Greenfield
<ubershmekel at gmail.com> wrote:
> On Mon, Jan 28, 2013 at 5:45 PM, Guido van Rossum <guido at python.org> wrote:
>>
>> Hm. I'm not keen on precomputing all of that, since most protocols
>> won't need it, and the cost add up. This is not WSGI. The protocol has
>> the transport object and can ask it specific questions -- if through a
>> general API, like get_extra_info(key, [default]).
>>
>
> I forgot to ask before, but why is get_extra_info better than normal
> attributes and methods?
>
> val = transport.get_extra_info(key, None)
> if val is None:
>     pass
>
> # vs
>
> if hasattr(transport, key):
>     val = transport.key
> else:
>     pass

hasattr() smells bad. It also has namespace issues (hasattr(transport,
"write") returns true) and if people forget to use it (perhaps because
the transport they normally use always has a certain attribute) their
code is brittle. Defining a new API with a string key signals clearly
that the value may or may not exist, and reminds people to test the
result for None. (Of course they can forget that too. But it still
feels different to me.)

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



More information about the Python-ideas mailing list