Since variables are just references, can we say that a variable has a data type, or is it more correct to say that the value it references has a type?
-
3Variables do not have data types in Python. – khelwood Jan 29 '21 at 16:12
-
I believe that this could go either way. It's a question of semantics...what is meant by "variable". I think it could be argued either way...that the variable has no type, or that the variable's type is defined by what it is referencing. What about `x=3`. Now is `x` an integer variable, since `3` is not a reference? – CryptoFool Jan 29 '21 at 16:15
-
2This is a semantics infinite-loop kind of question. Bottom line, variables are nothing but names attached to objects; objects have types. Spend some time with the Python [data model](https://docs.python.org/3/reference/datamodel.html). Also [here](https://medium.com/swlh/a-deep-dive-into-variables-in-python-8f55f69c3653) – dawg Jan 29 '21 at 16:20
-
1In `x = 3`, `x` is a variable whose value happens to be an integer, not an integer variable. If `x` were an integer variable, then `x = "foo"` would fail. – chepner Jan 29 '21 at 16:22
2 Answers
The values inside variables do have types, but you do not need to declare them: https://docs.python.org/3/library/datatypes.html
You can read the type of a specific value referenced through a variable with
a = 3
type(a)
but you can still redeclare a with everything else afterwards, just try it out in interactive python shell.
a = "bread"
a = [1, "tmp", (1,2)]
a = 0.1
...
EDIT: Yes, variables DO NOT have an own type, because variables only refer to a specific location in memory, where data (in a specific type) is stored. "Changing" a from 3 to "three" does not change the type of the variable, it allocates new memory to store the value "three" with the type of string and deletes the reference to the value 3.

- 444
- 4
- 14
-
1
-
3Yes: ignore anyone who says a variable has a type; it does not. A variable is just a name bound to a value. – chepner Jan 29 '21 at 16:23
-
1*So the values do, but the variables don't, right?* The **correct** question is **So the object (the values) do, but the the names (the 'variables') don't, right?** Right! – dawg Jan 29 '21 at 16:35
-
1"You can read the type of a variable..."—That is the type of *value* held by a variable. Not the type of the variable. – khelwood Jan 29 '21 at 16:47
In python we can say that the data-type of the variable depends on the value assigned;
e.g,
some_varible = 'some string' #here the variable is a string
some_other_variable = 3 # here it is int
But we didn't explicitly set the type.

- 4,084
- 2
- 19
- 34