Awful code of the week

Tim Delaney timothy.c.delaney at gmail.com
Sun Aug 7 18:03:08 EDT 2016


On 7 August 2016 at 16:54, Steven D'Aprano <
steve+comp.lang.python at pearwood.info> wrote:

> Seen in the office IRC channel:
>
>
> (13:23:07) fred:     near_limit = []
> (13:23:07) fred:     near_limit.append(1)
> (13:23:07) fred:     near_limit = len(near_limit)
> (13:23:09) fred: WTF
>

Assuming you'e not talking about the semantics of this specific code (which
could be reduced to near_limit = 1), I have to say I've been guilty of
reusing a name in precisely this way - set up the structure using the name,
then set the name to a calculated value.

I would only do this when I'm totally uninterested in the structure itself
- it's purely a way to get to the final result, but getting to that final
result requires several steps that justifies an intermediate name binding.
It avoids namespace pollution (but of course that can be fixed with del)
and avoids having to think up another name for a very temporary structure
(names are hard). And I would include a comment explaining the reuse of the
name.

The alternative would be something like (replace first line by something
complex ...):

near_limit_list = [1]
near_limit = len(near_limit_list)
del near_limit_list

Tim Delaney



More information about the Python-list mailing list