News for hackers. Stuff that matters.
Re:Python Lists vs. Tuples by colanderman on Friday August 19, 2016 @09:07PM (#12324540)
Oh, of course. In Python's case, I'd advocate to just get rid of tuples and be done with it. (Maybe replace it with a nice immutable adaptor class; I'm sure one already exists.) It's a dynamically typed interpreted language, neither performance nor type-correctness is its strong point.
Re:Python Lists vs. Tuples by pdonis on Friday August 19, 2016 @09:29PM (#12324613)
> In Python's case, I'd advocate to just get rid of tuples and be done with it.

That would be a bad idea for at least two reasons: sometimes you want containers to be immutable, and mutable containers can't be used as dictionary keys.

Re:Python Lists vs. Tuples by kazinator on Friday August 19, 2016 @09:35PM (#12324631)
You mean in Python or in computer science?

Because I'm pretty sure that it's possible to use mutable objects as keys in general. The users just have to observe restrictions: like don't insert the keys into the dictionary and then mutate them while they are in the dictionary and expect the dictionary to still work fine.

(Objects can also be used as keys even if they are mutated while in the dictionary, if identity-based equality is used for the dictionary; i.e. two keys are equal if they are acually one and the same object, and not just two similar objects.)

Re:Python Lists vs. Tuples by catnaroek on Friday August 19, 2016 @10:36PM (#12324865)
> Because I'm pretty sure that it's possible to use mutable objects as keys in general. The users just have to observe restrictions: like don't insert the keys into the dictionary and then mutate them while they are in the dictionary and expect the dictionary to still work fine.

In other words, mutable objects don't work as keys, and pretending they do requires unenforced conventions to preserve the integrity of the dictionary abstraction.

> Objects can also be used as keys even if they are mutated while in the dictionary, if identity-based equality is used for the dictionary; i.e. two keys are equal if they are actually one and the same object, and not just two similar objects.

The fundamental problem is very simple: dictionary keys have to be values, not objects, and Python simply doesn't have compound values. There is no such thing as “the list [1,2,3]” in Python: you just have one or more objects whose current state happens to be the sequence 1,2,3. The very next moment, their states might be different sequences. Object identities are first-class values in Python, but identities are primitive and indecomposable - not compound!

Re:Python Lists vs. Tuples by kilink on Friday August 19, 2016 @10:11PM (#12324756)
This is a Python specific restriction. Ruby for example allows mutable keys in dictionaries.
Re:Python Lists vs. Tuples by catnaroek on Friday August 19, 2016 @10:20PM (#12324799)
So, which is the case?

(0) Object identities are used as keys? (e.g., two different list objects that contain the elements 1,2,3 are treated as different keys)

(1) Catastr... I mean, hilarious things happen if you mutate an object that's being used as a key?

Re:Python Lists vs. Tuples by kilink on Friday August 19, 2016 @10:40PM (#12324888)
I'm not sure if you are asking about Python, or Ruby.

Anyway, both languages use value-based hashing for dictionaries. I am not a Rubyist, but I guess you are just expected to not mutate the keys? Ruby's dictionary implementation also has a rehash method to deal with this case [1], if for some reason you are purposely mutating the keys I suppose...

[1] http://docs.ruby-lang.org/en/2.0.0/Hash.html#method-i-rehash

Re:Python Lists vs. Tuples by catnaroek on Friday August 19, 2016 @10:56PM (#12324948)
> I'm not sure if you are asking about Python, or Ruby.

Ruby, since you said it allows mutable keys in dictionaries.

> Anyway, both languages use value-based hashing for dictionaries.

Neither Python nor Ruby has compound values. Object identities are primitive values.

> but I guess you are just expected to not mutate the keys?

I guess C programmers are expected not to use memory after they free it.

Re:Python Lists vs. Tuples by kazinator on Friday August 19, 2016 @10:24PM (#12324810)
But if Python got rid of tuples, it would be a radical change; in that case it could as well allow lists to be dictionary keys in the same stroke.
Re:Python Lists vs. Tuples by colanderman on Friday August 19, 2016 @09:56PM (#12324712)
Yes, hence my suggestion to introduce an immutable adaptor class. Then all containers (sets, dictionaries, etc.) gain those abilities.
Re:Python Lists vs. Tuples by pdonis on Friday August 19, 2016 @11:25PM (#12325016)
> hence my suggestion to introduce an immutable adaptor class

What does this gain you over just doing tuple(obj) (or the equivalent--tuple(d.items()) for a dictionary, for example) when you want to make it immutable for use as a dict key?

Re:Python Lists vs. Tuples by colanderman on Friday August 19, 2016 @11:37PM (#12325060)
It can be implemented copy-on-write. It can have more compact storage if the runtime is smart. It can continue to efficiently support lookup (e.g. tuple(d.items()) would not).
Re:Python Lists vs. Tuples by pdonis on Saturday August 20, 2016 @11:13AM (#12326931)
> It can be implemented copy-on-write.

Which only makes sense if the underlying mutable object is mutated. But if the underlying mutable object is mutated, the adaptor instance, if it is immutable, can no longer have the same value as the underlying mutable object. So the "adaptor" is no longer an adaptor; it's just a separate immutable object with the value that the mutable object had originally. I.e., it's just the same as tuple(d.items()) or whatever.

If by "copy-on-write" you just mean that two different containers that both store the same immutable value, say a string, can both point to the same memory storing that value, and the mutable container's pointer only changes if a new value is stored at that key/index, Python already does that; for example, tuple(d.items()) does not create new storage for the items themselves, it points the tuple to the same objects that are contained in the dict at the time of the call to d.items().

> It can have more compact storage if the runtime is smart.

A tuple in Python does have more compact storage than a list, dict, or set.

> It can continue to efficiently support lookup

Same issue as above; this only matters if the underlying object is mutated, in which case an immutable "adaptor" can no longer have the same value, so lookup on the adaptor isn't the same as lookup on the underlying mutable object. Lookup on the adaptor is just lookup on the immutable value, which is easily made efficient.

Re:Python Lists vs. Tuples by philipov on Friday August 19, 2016 @09:55PM (#12324711)
It's worth noting that tuples aren't actually immutable in Python. Tuples have immutable length, but the values in those slots can be changed at runtime.
Re:Python Lists vs. Tuples by colanderman on Friday August 19, 2016 @09:58PM (#12324717)
No, they definitely can't:

    >>> (1,2,3)[2]=5
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
I wish they could. (Maybe Python 3 is different?)
Re:Python Lists vs. Tuples by shoyer on Friday August 19, 2016 @10:15PM (#12324772)
You can't replace a value in a tuple, but you can modify it, e.g.,

  >>> x = (1, 2, [3])
  >>> x
  (1, 2, [3])
  >>> x[2].append(4)
  >>> x
  (1, 2, [3, 4])
Re:Python Lists vs. Tuples by kilink on Friday August 19, 2016 @11:17PM (#12324989)
...Or can you?

  import ctypes

  def tuple_setitem(t, index, item):
      obj = ctypes.py_object(t)
      item = ctypes.py_object(item)
      ref_count = ctypes.c_long.from_address(id(t))
      original_count = ref_count.value
      if original_count != 1:
          ref_count.value = 1
      ctypes.pythonapi.Py_IncRef(item)
      ctypes.pythonapi.PyTuple_SetItem(obj, ctypes.c_ssize_t(index), item)
      ref_count.value = original_count

  >>> x = (1, 2, 3,)
  >>> tuple_setitem(x, 1, "foo")
  >>> x
  (1, "foo", 3)
Disclaimer: for entertainment purposes only, do not try this at home!
Re:Python Lists vs. Tuples by nemothekid on Friday August 19, 2016 @10:39PM (#12324878)
You haven't changed anything about the tuple. The holds a reference to a list and will always hold a reference to that list.

What you did was modify the underlying list, the tuple itself is immutable.

Re:Python Lists vs. Tuples by pdonis on Friday August 19, 2016 @11:27PM (#12325020)
> You can't replace a value in a tuple, but you can modify it

If it has mutable items in it, yes. But in that case, the tuple can't be used as a dictionary key; Python checks, not just that the tuple itself is immutable, but that every object in it is immutable.

Re:Python Lists vs. Tuples by gakada on Friday August 19, 2016 @10:11PM (#12324758)
Tuples are immutable so that they can be used as keys in hash tables.
Re:Python Lists vs. Tuples by kazinator on Friday August 19, 2016 @10:26PM (#12324822)
But a sibling comment to yours by shoyer argues that mutable objects can be elements of a tuple. A tuple whose elements are mutable is effectively mutable.

Can (1, 2, [3]) be used in a hash table, and what if the [3] is mutated to [3, 4]?

Re:Python Lists vs. Tuples by philipov on Friday August 19, 2016 @10:42PM (#12324899)
When it tries to hash the tuple, it finds the list inside of it, realizes the tuple is mutable, and throws an error.
Re:Python Lists vs. Tuples by nerdponx on Friday August 19, 2016 @09:45PM (#12324662)
I use them as the semantic guide when I write code.

Then again, I also use single and double quotes as a semantic guide to mean different things. I feel like that would probably make most programmers want to throw the computer at the wall, but so it goes

Re:Python Lists vs. Tuples by colanderman on Friday August 19, 2016 @09:52PM (#12324692)
I do the same! But I get frustrated that I can neither modify individual elements of my "cultural" tuples, nor is there an immutable (functional) "cultural" list (or dictionary, for that matter).
Re:Python Lists vs. Tuples by philipov on Friday August 19, 2016 @09:51PM (#12324691)
what, of course, single quotes are for characters and double quotes are for strings.
Re:Python Lists vs. Tuples by markrages on Friday August 19, 2016 @10:28PM (#12324836)
C programmer identified.
Re:Python Lists vs. Tuples by omaranto on Friday August 19, 2016 @10:14PM (#12324765)
What do you use single and double quotes for?
Re:Python Lists vs. Tuples by SpaceManiac on Friday August 19, 2016 @10:25PM (#12324818)
I can't speak for GP, but I like to use single quotes for symbolic strings ('go_left', 'parent', 'alpha-campus', some_map['fixed_key']) and double quotes for textual strings ("An error occurred", "package/__main__.py").
Re:Python Lists vs. Tuples by BerislavLopac on Saturday August 20, 2016 @07:30AM (#12326091)
Sometimes this convention falls apart, e.g. when constructing HTML tags it is easier (and more readable) to write:

    return '<a href="{}">{}</a>'.format(url, text)
than

    return "<a href=\"{}\">{}</a>".format(url, text)
Especially for a lazy developer like me... :)
Re:Python Lists vs. Tuples by gsnedders on Saturday August 20, 2016 @04:28PM (#12328178)
Why not just use single-quotes in the HTML?
Re:Python Lists vs. Tuples by nerdponx on Monday August 22, 2016 @01:06PM (#12337730)
Fair. But I would consider those to be unformatted text, not symbols.
Re:Python Lists vs. Tuples by omaranto on Friday August 19, 2016 @10:31PM (#12324846)
Oh, I like that idea: compensate via a convention for Python's lack of a symbol data type. :)
Re:Python Lists vs. Tuples by jholman on Saturday August 20, 2016 @12:35AM (#12325230)
If you mean Ruby's symbols, Python has a data type with that set of properties. It's called 'str'.

If you want the behaviour of Ruby strings, you might use a `bytes` or `bytearray` type, or you might use a list of chars.

Re:Python Lists vs. Tuples by omaranto on Saturday August 20, 2016 @01:50AM (#12325397)
I thought Ruby ensured there was only one symbol with given contents, so that equality testing for symbols reduces to pointer equality. I don't Python strings behave that way.

But at any rate I wasn't thinking of performance characteristics but rather the stereotypical uses of symbols vs strings in languages that have both, so you are right that one can use Python strings in place of symbols, as SpaceManiac already pointed out above.

Re:Python Lists vs. Tuples by BerislavLopac on Saturday August 20, 2016 @07:26AM (#12326078)
> I thought Ruby ensured there was only one symbol with given contents, so that equality testing for symbols reduces to pointer equality. I don't Python strings behave that way.

In Python, string literals are reused, if that is what you meant.

    >>> a = 'abc'
    >>> b = 'abc'
    >>> a is b
    True
Re:Python Lists vs. Tuples by gsnedders on Saturday August 20, 2016 @04:29PM (#12328185)
This isn't guaranteed by the language, even if CPython does it. What is guaranteed is intern('abc') is intern('abc').
Re:Python Lists vs. Tuples by infinite8s on Saturday August 20, 2016 @11:15AM (#12326937)
String literals up to a certain size are reused.
Re:Python Lists vs. Tuples by omaranto on Saturday August 20, 2016 @02:06PM (#12327659)
Oh, cool, I didn't know that. I think in Ruby all symbols are reused, whether they are literals or constructed programatically. (Unlike Common Lisp symbols which can also de 'uninterned' if they are not literals.)
Re:Python Lists vs. Tuples by nerdponx on Saturday August 20, 2016 @12:44AM (#12325261)
Yup I do the same.

some_dict = {'key': "value"}