News for hackers. Stuff that matters.
Re:Anti-Patterns in Python Programming by bkeroack on Wednesday July 09, 2014 @10:02AM (#8009642)
I'm not sure what you're getting at here, or what you are expecting tuples to be like. They can have as much "structure" as you need--they're just a collection.

Lighter-weight, immutable collections have a use case. The code in OP appears to be one where it makes sense. I follow the rule where variables are mutable IFF they need to be mutable.

Re:Anti-Patterns in Python Programming by dozzie on Wednesday July 09, 2014 @10:58AM (#8009943)
Tuples by pythonists are used as they were mere lists, just immutable. This is clearly displayed by Python's own interface.

For the rest of the world, tuples are not immutable lists. They are tuples, i.e. collections of "objects" that could share nothing about their type. Tuples often are not even iterable! (Erlang, Haskell)

The fact that tuples in Python can have as much structure as one wants is derived from dynamic typing, not from the tuples' nature. The same you could say about Python's lists.

This is a really subtle issue. It takes to know more languages to see it clearly.

Re:Anti-Patterns in Python Programming by maxerickson on Wednesday July 09, 2014 @03:37PM (#8011789)
Have you looked at named tuples? They shipped in the python standard library sometime in the last few years (they are at least in 3.3) and are clearly intended for storing structured data.

A typical rule of thumb in Python land is that heterogeneous data probably belongs in a tuple, so practice goes a little further than immutable lists.

I think you could improve your demonstration of the usage in the standard library by examining a random selection of usages to try to find out what is typical. But maybe you already looked at more than you talk about in the article (and I understand that this might not be an interesting use of your time).

Re:Anti-Patterns in Python Programming by dozzie on Thursday July 10, 2014 @04:25AM (#8014166)
No, I haven't looked at them. Python 2 has them since release 2.6, so it's out of my reach for any practical purpose at the moment (I need to preserve compatibility with Python 2.4).

> A typical rule of thumb in Python land is that heterogeneous data probably belongs in a tuple, so practice goes a little further than immutable lists.

The problem with Python tuples is it's two things mixed: immutable lists and a container for heterogenous data. It's the same situation as JavaScript's objects.

Re:Anti-Patterns in Python Programming by bkeroack on Wednesday July 09, 2014 @12:32PM (#8010548)
If it's so subtle, does it matter? This sounds like you just have a problem with the word "tuple" applied to an object that behaves differently from tuples in a statically-typed language.

Would you feel better if they named it "ImmutableList" instead?

Re:Anti-Patterns in Python Programming by jholman on Wednesday July 09, 2014 @06:38PM (#8012612)
Can't speak for GP, but I would [feel better with that name].

(Although I agree with you that statically-typed-language-tuples don't seem to make sense in Python.)

But hey... Python's weird choice of how to name the ImmutableList could be worse, right?

For example, someone could be malicious enough to call their general-purpose associative array a "hash", just because a hashmap (note: not a hash) is a good implementation for large associative arrays. Wow, that'd be hilariously misleading, wouldn't it? Good times!

Or imagine someone was silly enough to name their auto-resizing arrays "vectors", even though in all previously existing contexts a "vector" is a sort of thing which absolutely cannot be meaningfully resized/extended. Ha. Think of the tiny cognitive burden placed on generations of future programmers-who-study-math, trying to juggle these two very-similar-but-distinct concepts, multiplied by the number of such future programmers. Amazing practical joke, right?

/rant

Re:Anti-Patterns in Python Programming by wtetzner on Wednesday July 09, 2014 @08:47PM (#8013016)
Not really that important, but I think map is a better name than associative array.
Re:Anti-Patterns in Python Programming by dozzie on Thursday July 10, 2014 @04:20AM (#8014143)
No, it's not a problem with word "tuple" behaving differently from statically-typed language. It's a problem with word "tuple" behaving differently from all the rest of the world.

Yes, I would feel better if it was named "ImmutableList" or any other way that is not misleading about the purpose.

Re:Anti-Patterns in Python Programming by jl6 on Wednesday July 09, 2014 @12:45PM (#8010615)
I'm sorry, I don't see clearly what a tuple should be. What would be different about Python tuples if they were true tuples?
Re:Anti-Patterns in Python Programming by alextgordon on Wednesday July 09, 2014 @06:25PM (#8012566)
In most languages you can't usually:

1. Iterate over a tuple

2. Convert a list to a tuple

3. Construct a tuple of a length not known at compile-time

Python allows these because "why not?" but it does break their "one and only one way to do it" rule and confuses beginners a hell of a lot.

There are definitely borderline cases. For instance, should a Vector be a list or a tuple? A Vec3 type is obviously a tuple, but a large Vector destined for BLAS is obviously a list.

Re:Anti-Patterns in Python Programming by dragonwriter on Wednesday July 09, 2014 @07:31PM (#8012791)
> Python allows these because "why not?"

No, it allows them because the distinction that those restrictions are founded on is only useful in a statically-typed languages, and Python isn't statically typed.

> For instance, should a Vector be a list or a tuple?

A real vector/array should be its own data type (probably implemented in a C, or similar low-level, extension) that happens to implement the interface expected of an indexable, iterable collection, neither a list nor a tuple.

Re:Anti-Patterns in Python Programming by dozzie on Thursday July 10, 2014 @04:18AM (#8014136)
> [this] distinction [...] is only useful in a statically-typed languages

...like Erlang.

Re:Anti-Patterns in Python Programming by dragonwriter on Thursday July 10, 2014 @09:56AM (#8014962)
Yeah, while snarkily made its a good point that Erlang does make use of it without being statically typed.

There is a deep difference that goes beyond use of tuples in language approach between Python and Erlang here where it comes to types in which Erlang, while dynamically typed, has a deep concern for types in its pattern matching system to make path decisions while Python is very much centered on using dynamic OO techniques -- how objects respond to messages -- to do that.

So I'd still say its the same kind of deep language approach difference at work.

Re:Anti-Patterns in Python Programming by jl6 on Thursday July 10, 2014 @02:53AM (#8013960)
So why does Python distinguish between a list and a tuple at all?
Re:Anti-Patterns in Python Programming by dragonwriter on Thursday July 10, 2014 @09:48AM (#8014930)
Because the distinction between a mutable list and an immutable list is still meaningful in a dynamic language like python.
Re:Anti-Patterns in Python Programming by hueving on Thursday July 10, 2014 @05:08AM (#8014297)
One is mutable, one is not. Performance. Also, look up namedtuple. Useful for returning multiple values.