
Python insights - pavel
http://eli.thegreenplace.net/programs-and-code/python-insights/
======
DaniFong
Here's one I only found out about recently.

Multiple assignment in Python is already well known.

    
    
      x, y = 10, 50
    

But this also works when the left or right variable lists are sequences.

    
    
      [x,y] = 10,4
    
      t = (1,2,3)
    
      a,b,c = t
    

If you have a loop or a comprehension, and the values are sequences, you can
access them like so

    
    
      for (left, right) in zip(leftArray,rightArray):
          ...
    

You can also do variable assignment inside an argument list. To see what I
mean, suppose you're writing a 2d geometry library, and you use tuples as
points. Ordinarily you might write

    
    
      def func(point):
          x, y = point[0], point[1]
          ...
    

or even

    
    
      def func(point):
          x, y = point
          ...
    

Instead, you can just write

    
    
      def func((x,y)):
          ...
    

This has many advantages. It gets around the problem of having to navigate to
some other place to find out what composite data structures _are_. Points are
just (x,y) tuples. It makes it easier to write sensible names, in many cases,
which is crucial in a dynamic language. It's shorter. You spend less time
packing, unpacking, and generally, schlepping your data, and more time
_actually doing things._

Unfortunately, it's not the most generic of methods. It fixes you to a
particular data representation. This is effective in certain cases, but is
otherwise a problem when you need to change things. In practice, though, this
can be avoided, and other code would break during such changes too.

~~~
qwph
Interesting. It seems that (according to my Python prompt)

    
    
      x, y = 1, 2
    

and

    
    
      [x, y] = 1, 2
    

and even

    
    
      x, y = [1, 2]
    

are all identical in function. Are there any (subtle) distinctions between the
three?

~~~
scott_s
The last is different in that it returns a list, not a tuple. This makes a
difference if you want to change the object.

    
    
      >>> tuple = x, y = 1, 2
      >>> type(tuple)
      <type 'tuple'>
      >>> tuple
      (1, 2)
      >>> tuple[0] = 1
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
      TypeError: object doesn't support item assignment
      >>> lst = x, y = [1, 2]
      >>> type(lst)
      <type 'list'>
      >>> lst
      [1, 2]
      >>> lst[0] = 3
      >>> lst
      [3, 2]

~~~
qwph
Aha! There's actually no difference (i think) between:

1, 2

and:

(1, 2)

That seems to make sense.

~~~
scott_s
I think that is true, but I think that it's only later versions of Python
(2.5+, maybe?) that allow you to omit the parenthesis. I've had earlier
versions (2.3?) complain of a syntax error when I leave them out.

------
BrandonM
From the article:

 _String reverse

Python doesn’t have a built-in reverse method for strings. Luckily, this can
be easily done with slices:

    
    
      def reverse(str):
          return str[::-1]

_

If you don't like str[::-1] (even though it is a common idiom), you could just
use

    
    
      "".join(reversed(str))
    

Is reversing a string really common enough to have a special function for it
with such a general name like reverse?

~~~
schtog
There is a list-reverse that changes the list and returns None but maybe the
reason there isn't one for strings are because they are immutable while lists
are mutable.

~~~
BrandonM
That's a good point. I was thinking more along the lines, though, that there
are some times where you actually need to reverse a list. How often does it
make sense to reverse a string? When palindrome testing?

