How Python works

How Python works

Python

———————-

module          module

——————————————————————

statement       statement       statement        statement

————————————————————————————————

expression      expression       expression       expression      expression       expression

——————————————————————————————————————————————–

object            object          object           object         object           object         object           object

CPython

It’s true for CPython that’s the standard version we use with the Python name. It’s based on ANSI C.

Source————————->Byte Code—————————>Runtime

plik.py                                            plik.pyc                                       Python Virtual Machine

writing                                            compilation                               interpretation

dependent on you                 automatic, independent            dependent on the system we use

Python looks for a plik.pyc, and then plik.py if the first one isn’t present.

Jython

Source—————————–>Similar to Java

plik.py

IronPython

Source—————————–>Similar to .NET

plik.py

To catch the output in a file:

python plik.py > plik.txt

To catch the input in a file:

python plik.py < plik.txt

Usually, we can run the file in Windows:
python plik.py
But when we have not set the PATH environment variable, we must provide the full path to our Python interpreter:
D:\python33\python plik.py

On new Windows, however, we can write (if our PATH is set):

plik.py

We can provide the full path to the folder with our files (if our PATH isn’t set):
python D:\python33\scripts\plik.py
If our PATH isn’t set for Python’s directory, Python, and our file is in other folder than we are working in, we use:

D:\Python33\python D:\python33\scripts\plik.py

There are plik.py and plik.pyw. The latter will display only a window made by our script, not DOS console window as usual. Such a file has a special behavior on Windows. It’s used for user interfaces.

We can import a Python module only once per session (process). We can use import many times — no changed will be. But we can reload the module.
>>> from imp import reload
>>> reload(plik)

A module is a package of variable names that is known namespace. These name in this package are know attributes (an attribute is a variable name that is attached to any object).

We have the following module:

python plik.py

And we can do:

Get the whole module:
>>> import plik
We like Polish. We love Polish.
>>>
>>> plik.b, plik.c
(‘We like Polish.’, ‘We love Polish.’)
>>>

Copy the names:
>>> from plik import a, b
>>> b, c
(‘We like Polish.’, ‘We love Polish.’)

We can see the values in parentheses because they are tuples. dir can get all names in a module:
>>> dir(plik)
[‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’, ‘a’, ‘b’]

A program is composed of many modules linked together by import statements. Each  module is a self-contained package of variables that is a namespace. One module doesn’t see the names in another file. However, we can import them

Remember — import is better than from!

When Python imports, it started to seek in sys.path – a Python list of directory name strings in the sys module, which is initialized form PYTHONPATH, and set of standard directories.

We can run the code in our module using:

exec(open(‘module.py’).read())

We don’t need import and reload our module. exec runs the current version of the file without reloading.
>>> exec(open(‘plik.py’).read())
We like Polish.
We love Polish.
>>> exec(open(‘plik.py’).read())
We like Polish.
We love Polish.
We worship Polish.
>>>

exec runs the module anew. It can overwrite variable we are using (like from). It’s better not to use that.

Leave a comment