3

In Python, if you want to define a variable, you don't have to specify the type of it, unlike other languages such as C and Java.

So how can the Python interpreter distinguish between variables and give it the required space in memory like int or float?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Iem-Prog
  • 593
  • 1
  • 5
  • 9
  • 3
    This is not specific to Python, but to any *dynamically typed language*. – deceze Oct 07 '16 at 13:04
  • 3
    And who told you in `PHP` you need to define the type of variable? – Kinshuk Lahiri Oct 07 '16 at 13:06
  • 1
    The object assigned to the variable doesn't affect the memory needed for the _variable_. The object itself is different from the variable. – khelwood Oct 07 '16 at 13:08
  • Also, *specifying* a type, *knowing* the type and reserving memory for it are all orthogonal concepts. Even statically typed languages can often *infer* the type of variables without needing them explicitly specified. `var foo = 'bar'` – of course `foo` is going to be a string, what else? – deceze Oct 07 '16 at 13:19

4 Answers4

7

In Python all values are objects with built-in type info. Variables are references to these values. So their type is 'dynamic', just equal to the type of what they happen to refer to (point to) at a particular moment.

Whenever memory is allocated for the contents of a variable, a value is available. Since it has a type, the amount of memory needed is known.

The references (variables) themselves always occupy the same amount of memory, no matter what they point to, since they just contain a conceptual address.

This indeed means that in

def f (x):
    print (x)

x doesn't have a type, since it doesn't have a particular value yet. The upside is that this is very flexible. The downside is that the compiler has only limited means to discover errors. For this reason Python was recently enriched with type hints. Tools like mypy allow static typechecking, even though the interpreter doesn't need it. But the programmer sometimes does, especially at module boundaries (API's) when she's working in a team.

Community
  • 1
  • 1
Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
6

Dynamically typed languages typically use boxed representation, which includes runtime type information. E.g. instead of storing direct pointers to a value, the system uses a box struct that contains the value (or pointer to it) as well as some additional metainformation. You can see how he standard Python implementation does it here: https://github.com/python/cpython/blob/master/Include/object.h

There are some interesting tricks that can be employed here. For instance, one technique is value tagging, where the type description is stored as part of the value itself, utilising unused bytes. For instance, pointers on current x86-64 CPUs can't utilise the full address space, which gives you some bits to play with. Another variant of this technique is NaN-tagging (I believe this was first used by Mike Pall, author of LuaJIT) - where all values are stored as doubles, and various NaN states of the value signal that it is actually a pointer or some other type of data.

MrMobster
  • 1,851
  • 16
  • 25
2

Python is dynamically typed language which means that the type of variables are decided in running time. As a result python interpreter will distinguish the variable's types (in running time) and give the exact space in memory needed. Despite being dynamically typed, Python is strongly typed, forbidding operations that are not well-defined (for example, adding a number to a string) .

On the other hand C and C++ are statically typed languages which means that the types of variables are known in compilation time.

Using dynamic typing in programming languages has the advantage that gives more potential to language, for example we can have lists with different types (for example a list that contains chars and integers). This wouldn't be possible with static typing since the type of the list should be known from the compilation time...).
One disadvantage of dynamic typing is that the compiler-interpreter in many cases must keeps a record of types in order to extract the types of variables, which makes it more slow in comparison with C or C++.

A dynamic typed language like python can be also strongly typed. Python is strongly typed as the interpreter keeps track of all variables types and is restrictive about how types can be intermingled.

coder
  • 12,832
  • 5
  • 39
  • 53
1

The Python interpreter analyzes each variable when the program runs. Before running, it doesn't know whether you've got an integer, a float, or a string in any of your variables.

When you have a statically typed language background (Java in my case), it's a bit unusual. Dynamic typing saves you a lot of time and lines of code in large scripts. It prevents you from having errors because you have forgotten to define some variable. However, static typing lets you have more control on how data is stored in a computer's memory.

adsalpha
  • 102
  • 2
  • 10
  • *It prevents you from having errors because you have forgotten to define some variable* – Wut? Trying to access an undefined variable is an error just as much in Python as in most other languages. It will simply surface at runtime instead of compile time (which is arguably worse). – deceze Oct 07 '16 at 14:15
  • I'm talking about such errors like forgetting to define an iterator over a loop in Java or assigning a value to a variable without defining it. Sure it would throw an error if you try to access what don't exist. – adsalpha Oct 08 '16 at 13:49