Programming newbie coming from Ruby: a few Python questions

Bruno Desthuilliers onurb at xiludom.gro
Wed Aug 2 05:25:49 EDT 2006


Luis M. González wrote:
> Delaney, Timothy (Tim) wrote:
(snip)
>> "Clever" in this context generally means using a trick/hack that is
>> non-obvious (often even after you understand it). "Cleverness" often
>> leads to difficult-to-understand code, which is contrary to the "python
>> philosophy".
>>
>> There are occasionally times when cleverness is useful, but in those
>> cases it should always be commented explaining exactly what it is doing,
>> and why a non-clever solution was not used. Most of the time there's
>> still a better non-clever way to do it, but the coder is not clever
>> enough to find it ;)
>>
> 
> Is this kind of cleverness 

Which one ?-)

> what is usually known as "magic"?
> I suspect that this has something to do with it, but not completely
> sure...

Python's "magic" is mostly about using the more advanced features of the
language (closures, special '__magic__' methods, descriptors,
metaclasses) and take full advantage of Python's object model and
dynamicity to reduce boilerplate and factor out repetition. All this
surely requires a good understanding of Python's object model - and may
look somewhat "magic" to newcomers -, but no particular "cleverness".

The key here is not to use these features for the sake of using them,
but only if and when it really simplifies the rest of the code by
factoring out the existing complexity.

The unpythonic "cleverness" is mostly about trying to stuff as much
operations as possible in a single expression. Which doesn't change
anything to the complexity of the code but makes the whole thing much
more difficult to understand - so it actually *adds* a level of complexity.

You can take a look at my sig for an example of unpythonic 'cleverness':

print '@'.join(['.'.join([w[::-1] \
                for w in p.split('.')]) \
                for p in 'onurb at xiludom.gro'.split('@')])


How much time do you need to understand what happens here ? Way too much
anyway. Now look at the dumbiest way to write the same thing:

parts = 'onurb at xiludom.gro'.split('@')
reversed_parts = []

for part in parts:
  words = part.split('.')
  reversed_words = []

  for word in words:
      # word[::-1] reverse the word,
      # ie "abc"[::-1] == "cba".
      reversed_words.append(word[::-1])

  reversed_parts.append('.'.join(reversed_words))

reversed_sig = "@".join(reversed_parts)
print reversed_sig

It's perhaps a bit longer, perhaps less 'elegant' (for a somewhat
perlish definition of 'elegance'), certainly less "clever", but at least
it's something that doesn't requires any special attention to understand
- except perhaps for the extended slice stuff, but it's explained by a
comment.


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list