By Hubro


2011-05-28 00:36:43 8 Comments

I assumed sequence types in Python were value types. It turns out they're reference types (Meaning that the value of a variable won't be copied when assigned to a new variable, but referenced). So now I'm wondering, what are the value types in Python? That is, what types in Python can I assign to new variables without worrying that the variable was referenced?

2 comments

@Eevee 2011-05-28 04:46:26

The answer above is correct, but I object to the semantics of "reference".

C-like languages treat variables as fixed buckets, in which values are placed. When you call a function, a new set of buckets are created, and the values are copied into them. Sometimes, a bucket is pass-by-reference, and actually becomes an alias for the caller's bucket.

Python, on the other hand, treats variables as mere labels (names) for values (objects). When you call a function, a new set of labels are created and slapped onto those same objects.

It doesn't make sense to mention "references" in the context of Python, because in every other language, "reference" implies an alternative to "value". Python has no such duality; it just passes around and assigns objects. Nothing is referred to.

Nitpicky, perhaps, but the terminology causes no end of confusion for C++ programmers, who e.g. hear Python passes by references and don't understand how to reassign the caller's names.

@Ignacio Vazquez-Abrams 2011-05-28 06:08:59

Python passes by value, but the values are references.

@Eevee 2011-05-28 06:12:12

That's silly. The passed values aren't references to objects; they ARE objects.

@cledoux 2011-05-28 13:53:38

Perhaps this article (bit.ly/4Cjmn0) and the SO question that linked to it (bit.ly/3fRXW) can shed some light on this. I find the terminology used in the article helps makes things a bit less confusing. Also, while I see your point about references, I disagree with it. A label or name is a reference, in the general sense of the word. When I use your name, I am referring to you.

@Jochen Ritzel 2011-05-28 14:08:44

I totally agree with you and I get into fights about this on SO sometimes ;-) If you know what a C-reference is then you know that Python cannot pass these, since that would make all objects mutable. I call Python's modus "call by aliasing", just to avoid questions like this one. On the other hand thinking that Python passes objects by reference works too: That reference is given by a string (the name of a object on the virtual machine) not by a number (a memory address on real hardware). Both views make sense and knowing them both helps more than either alone :-)

@Ignacio Vazquez-Abrams 2011-05-28 00:39:16

All values in Python are references. What you need to worry about is if a type is mutable. The basic numeric and string types, as well as tuple and frozenset are immutable; names that are bound to an object of one of those types can only be rebound, not mutated.

>>> t = 1, 2, 3
>>> t[1] = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

@Ned Deily 2011-05-28 00:58:59

For completeness it should be noted that, while a tuple is immutable, any mutable objects contained in it may be mutated: t = 1, [2], 3 ; t[1].append(42)

@Hubro 2011-05-28 01:19:03

For completeness, could you add how I would go about duplicating/cloning a value?

@Ignacio Vazquez-Abrams 2011-05-28 01:28:25

That depends on the type. For lists, you can either pass it to list() or slice the whole thing (L[:]). For sets, pass it to set(). For other types, see the relevant documentation. Note that there are types that effectively cannot be cloned at all, simply because they represent an external resource (e.g. socket).

@Jochen Ritzel 2011-05-28 03:33:36

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] What is the difference between @staticmethod and @classmethod?

53 Answered Questions

[SOLVED] Calling an external command in Python

39 Answered Questions

[SOLVED] What does the "yield" keyword do?

23 Answered Questions

27 Answered Questions

[SOLVED] What does if __name__ == "__main__": do?

30 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

15 Answered Questions

[SOLVED] What are metaclasses in Python?

10 Answered Questions

14 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

7 Answered Questions

[SOLVED] Proper way to declare custom exceptions in modern Python?

  • 2009-08-23 21:29:29
  • Nelson
  • 484877 View
  • 959 Score
  • 7 Answer
  • Tags:   python exception

Sponsored Content