[Tutor] Python internals
Steven D'Aprano
steve at pearwood.info
Sat May 4 15:32:27 CEST 2013
On 04/05/13 23:04, kartik sundarajan wrote:
> Hi,
>
> I am trying to learn how Python stores variables in memory. For ex:
>
> my_var = 'test'
>
> def func():
> pass
>
> when I type dir() I get
>
> ['__builtins__', '__doc__', '__name__', '__package__', 'func', 'help',
> 'my_var']
>
> are these variables stored in a dict and on calling dir() all the keys are
> returned?
> Or is it stored in a list or a heap?
Python objects are dynamically allocated in the heap.
Python "variables" are not variables in the C or Pascal sense, they are name bindings. When you do this:
my_var = 'test'
Python does the following:
- create a string object 'test'
- create a string object, 'my_var'
- use 'my_var' as a key in the current namespace, with value 'test'.
Creating a function is a little more complicated, but the simplified version goes like this:
- create a string object 'func'
- compile the body of the function into a code object
- create a new function object named 'func' from the code object
- use 'func' as a key in the current namespace, with the function object as the value.
When you call dir(), by default it looks at the current namespace. The "dunder" names shown (Double leading and trailing UNDERscore) have special meaning to Python; the others are objects you have added.
The documentation for dir says:
py> help(dir)
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
> Can anyone suggest if there some document I can read to help me understand
> the Python internals work ?
The Python docs are a good place to start.
http://docs.python.org/3/index.html
Especially:
http://docs.python.org/3/reference/datamodel.html
http://docs.python.org/3/reference/executionmodel.html
--
Steven
More information about the Tutor
mailing list