1

I was wondering what happens internally so that using Python you don't need to declare a variable type before assign it to something. Other languages like C reserve memory prior to the assignment phase.

I was just wondering what happens on compilation time so that Python can skip that step?

Mary
  • 153
  • 2
  • 2
  • 9
  • possible duplicate http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference/986145#986145 – markcial Feb 01 '14 at 18:29
  • Side note: Python isn't a compiled language. – b2Wc0EKKOvLPn Feb 01 '14 at 18:30
  • @ylabidi It is. What is commonly called the "interpreter" is actually a bytecode compiler. – Fred Foo Feb 01 '14 at 18:30
  • @ylabidi Well, it's typically compiled to bytecode. Untyped bytecode, so there's that. Also PyPy. –  Feb 01 '14 at 18:30
  • 2
    Python stores references; those are always the same type. And on a big memory heap *objects* are stored, that themselves keep track of what type they are. The Python compiler doesn't need to do anything about this, that's the job of the interpreter instead. – Martijn Pieters Feb 01 '14 at 18:30
  • @MartijnPieters the references are addresses for memory allocation, right? But when you write a=5 then what happens. How does it know it's a number. Can you talk me through it? Thank you. – Mary Feb 01 '14 at 18:38
  • @Mary: The compiler recognizes `5` as a int literal, and the bytecode stores a new `int(5)` object as a constant with the code, which when run binds the name `a` to that constant. The Python interpreter interns small integers, so even the constant will be a reference to a pre-existing `int(5)` object, because small integers are used often and immutable, so you may as well just reference the same object. That small object lives on the memory heap Python creates for this purpose. – Martijn Pieters Feb 01 '14 at 19:08
  • @Mary: The objects on the heap are all C struct values, see the [C API](http://docs.python.org/2/c-api/). – Martijn Pieters Feb 01 '14 at 19:09

3 Answers3

1

If you look deep down at the C level, all Python objects are of type PyObject*, and allocated on the heap. Variables are just names (usually implemented as hash map entries) you bind to these objects.

Max Noel
  • 8,810
  • 1
  • 27
  • 35
  • So for example, if I do `a=5` then a PyObject* is created which allocates the same memory every time? Thank you @MaxNoel – Mary Feb 01 '14 at 18:49
  • Integers are a special case -- they're `PyObject*` too, but since they're very common and immutable, they're cached. So each integer will only be allocated once, no matter how many times you use it in your program. Lists, dicts, sets and custom objects, though, are allocated just like you said, yeah. – Max Noel Feb 01 '14 at 18:52
0

Well, I think the python don't skip that step, what he does is look for the value inside the variable and then he should knows what type of variable it is. Like:

a = 5
b = 5.0

Python will look, what value is inside the a, well it's five, so lets make this variable an integer! And then move to the next variable.

ranu
  • 622
  • 15
  • 25
0

Variables in Python are just references to objects. The memory for an object is allocated at runtime in the moment that the object is instantiated.

Ilie NEACSU
  • 530
  • 3
  • 12