variable declaration

Antoon Pardon apardon at forel.vub.ac.be
Mon Feb 7 08:00:32 EST 2005


Op 2005-02-05, Nick Coghlan schreef <ncoghlan at iinet.net.au>:

> [ ... ]
>
> With a rebinding operator, the intent of the last line can be made explicit:
>
> def collapse(iterable):
>      it = iter(iterable)
>      lastitem = it.next()
>      yield lastitem
>      for item in it:
>          if item != lastitem:
>              yield item
>              lastitem .= item
>
> (Note that doing this *will* slow the code down, though, since it has to check 
> for the existence of the name before rebinding it)

I think that most of the time it won't slow down the code as the
checking will have been done during the compilation phase. It
may even speed up code like this, because the compilation
phase will have established that it is a rebinding and so
code for testing whether a new variable is created or not
is not necessarry here.

-- 
Antoon Pardon



More information about the Python-list mailing list