On 11/17/2016 12:40 AM, Veek M wrote:
In C:
int x = 10;
results in storage being allocated and type and location are fixed for
the life of the program.

In Python,
x = 10

causes an object '10' to be created but how exactly is 'x' handled?
Symbol Table lookup at compile time?

Modules and class names are set into and gotten from a symbol table at *runtime* (except that module globals start with some initial reserved names). Globals() returns the modules symbol table, which is a dict. To see the initialization, execute "print(globals())" on startup. For CPython 3.6.0b3 executed via IDLE's shell:

>>> globals()
{'__name__': '__main__',
 '__doc__': None,
 '__package__': None,
 '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
 '__spec__': None,
 '__annotations__': {},
 '__builtins__': <module 'builtins' (built-in)>}

Class dicts are created at runtime and also have default entries.

>>> class C: pass

>>> print(C.__dict__)
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'C' objects>, '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None}

>>> print(dir(C))  # include attributes inherited from object.
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Is every 'x' being substituted out of existence?

In CPython, function local names are replaced by int indexes into a list of objects, but still 'exist'. They are kept with the code object as a list. It is used for printing locals() and for disassembly output. The latter is deceptive in this regard if you do not know that the names are substituted back for the indexes).

The local namespace, an attribute code does not have any default entries.

>>> def f(): print(locals())

>>> f()
{}

The code object itself and the function object that wraps a code object both have numerous special-name attributes.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to