[Tutor] recursion

Ben Finney ben+python at benfinney.id.au
Fri Feb 5 04:07:29 EST 2016


Alan Gauld <alan.gauld at btinternet.com> writes:

> On 05/02/16 02:03, noopy via Tutor wrote:
>
> > def permutations(items):
> >      n = len(items)
> >      if n==0: yield []
> >      else:
>
> I assume this bit is clear enough?

I think it would be clearer without the needless opaque name ‘n’.
Better::

    def permutations(items):
        if not items:
            # ‘items’ is empty (or is not a container).
            yield []
        else:
            for i in range(len(items)):
                …

Binding a name that is used exactly once should be done only if the name
clarifies the purpose. An opaque name like ‘n’ is not helpful.

-- 
 \     “Why doesn't Python warn that it's not 100% perfect? Are people |
  `\         just supposed to “know” this, magically?” —Mitya Sirenef, |
_o__)                                     comp.lang.python, 2012-12-27 |
Ben Finney



More information about the Tutor mailing list