0

Are all references variables and all variables references in Python?

Obviously there's a difference between a variable and a reference in e.g. C++ but what about Python?

Noelkd
  • 7,686
  • 2
  • 29
  • 43
  • I have to say I am struggling to understand the question. Can you give some examples of what you mean exactly? Both Python and C++ examples would be helpful. – NPE Jun 05 '14 at 11:51
  • In C++ there are so called "reference variables", which definitely can be called both a variable and a reference. An example: int a = 5; int& ref = a; Not all variables are "reference variables" in C++ though. I've been just wondering if in Python there is some special case in which the term 'variable' and 'reference' wouldn't overlap –  Jun 05 '14 at 13:21

1 Answers1

2

Although even Python documentation mixes the terms, the reference documentation uses the terms identifiers or names instead of variables. It's the same concept though.

All identifiers are references; all values in Python are objects whose lifetime is governed by how many references exist to those objects; objects whose reference count drops to 0 are cleaned up.

However, not all references are identifiers. List indices are also references, as are the keys and values in a dictionary, and the attributes on an object.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    I am not sure I agree with the first statement. For example, https://docs.python.org/2/library/functions.html repeatedly uses the term "variable". – NPE Jun 05 '14 at 11:49
  • 1
    Yeah, I second that. Python (both official sources and many community members of all skill levels) uses the term "variables", for better or for worse. It might be better if they didn't, but oh well. –  Jun 05 '14 at 11:50
  • @NPE: ah, yes, the documentation being inconsistent. I'll alter that. – Martijn Pieters Jun 05 '14 at 11:51
  • What is missing from the answer is the explicit statement that all identifiers *are* references. Python has no "primitive value" type like Java's `int` (or whatever those are called). – Fred Foo Jun 05 '14 at 11:55
  • "all values in Python are objects" I would say that all values in Python are pointers to objects. "Objects" are not values. A "value" is the result of evaluating an expression, and has the same type as the type of that expression. A dynamically-typed language like Python can be considered to have one static (compile-time) type. What kind of type is this? Variables are expressions; variables and other expressions in Python have reference (pointer to object) type. Therefore the values must also have reference (pointer-to-object) type. Python is semantically identical to reference types in Java. – newacct Jun 05 '14 at 23:25
  • Really, that's just complicating things. And variables are *not* expressions. Referencing an identifier can be (part of) an expression but that doesn't make it equivalent. Assignment is a statement in Python, and the identifier or identifiers used as assignment targets are *not* expressions. – Martijn Pieters Jun 05 '14 at 23:35