0

I'm writing some formal semantics for Python. Hopefully some Python guru's can help me out. As program execution moves down through a list of statements, each assignment statement (or expression) it encounters, it infers the type based on context.But does it store this type somewhere then (so the next time in encounters it can check the store and check the type), or does it perform the operation, then throw away the type, and then infer the type again the next time it encounters the variable? This is for Python 3+ btw (hopefully I've made that clear)

John Setter
  • 175
  • 1
  • 17
  • 3
    What do you mean by *it infers the type based on context*? – bereal Apr 28 '16 at 17:21
  • well if there is a variable a.. and the statement is a = 2.. then it infers a is of type integer.. or if it is "a = 2 + 2.2" then by the context it infers a is of type float – John Setter Apr 28 '16 at 17:41
  • well its not much of a inferring to think of a integer as a integer. Its not so much inferring anything as that's how integers are defined in python its just as strong definition as `int a = 10;` if you want it to be something other than a integer then you have to declare it just like in any other language. Just that floats, integers, strings, bytes, tuples, lists, dictionaries and imaginary numbers have shorthand declarations – joojaa Apr 28 '16 at 17:46

1 Answers1

1

Python uses dynamic typing. The type is part of the value. Wherever a value goes, its type goes with it. (You can get an independent value that corresponds to that type with the type() function.) A value's type is only thrown away when the whole value is thrown away. A variable is just a reference to a value: each variable refers to exactly one value, and in general it can be any possible value, with no restrictions on type.

For example, in

a = 2

Python doesn't make a an int variable; 2 is what has the type int. a is just a reference to 2, which happens to be an integer. When you then write

b = a + 3

Python gets the type of a by simply getting the value referred to by a, and then getting the type of that. The expression is evaluated and the result stored in b.

From the point of view of a statically typed language, it's as if every variable in Python has the same type value, thus each holds a value.

For more information on static and dynamic typing, see this other question.

Community
  • 1
  • 1
Dan Getz
  • 8,774
  • 6
  • 30
  • 64
  • So, in Java there is a store, or a state, which knows the type of an object. So you cant say "int i = 1.... i = true" because the type of i is int... In Python you can do that because it will re declare the variable. So there is no state like that, from your answer I get the idea "a = 2" .. 2 is stored in the memory address a points to. and then when I perform an operation on a, it looks up the address, see's a is of type integer, then evaluates the operation ? – John Setter Apr 28 '16 at 17:46
  • Right, variables do not have types in Python like they do in Java. Nor do they need to be declared before being set. The set of variables acts very much like a `Map` in Java. (In fact you can get a value very much like this map by calling `vars()`.) – Dan Getz Apr 28 '16 at 17:49
  • This confusion is the reason why I prefer the term "name" over "variable" in Python. The word "variable" makes it seem like it's something rather than a reference to something. – Jared Goguen Apr 28 '16 at 17:53