- gcc compiles your c code and generates an executable code. This code has machine instructions, means CPU can understand these instructions. Kernel has to load the executable code into memory and point CPU to starting instruction.
- java converts the java code into byte code and this byte code is executed on JVM (written in C).
- JVM understands byte code and perform actions based on byte code instructions.
- Python compiles your code and generates a byte code. This is not a machine code, it is python representation. This byte code is executed on Python virtual machine (PVM). PVM is the run time environment of python, it is installed as part of your python installation. This process is hidden to users who are executing .py file with python binary. Python also generates .pyc (compiled python) files We can ship the .pyc files as your product. It might be easy to decode the python source from pyc. python can not be used to delivery a proprietary product where they don’t want to expose alogs or logics involved. There is a concept of frozen binaries, which packages .pyc, PVM and some support files and provide a .exe file. This is how python applications are shipped. You no need to install Python on your machine.
- To just compile python code and create .pyc files,
python -m py_compile script.py
- Since object code is not a machine code, Byte code instruction execution takes more time because they are not executed directly on CPU. PVM needs to understand these byte code instructions and take action.
- There is Jpython, which converts your python program to Java byte code and run these java byte code on JVM.
I started reading/executing the examples given in Dive into Python 3 book by Mark Pilgrim. After reading the first chapter I felt that I need to take some notes on the basics. Some of the basics may be rarely remembered or talked once you make progress on coding in python. I cautiously look for those kind of points and list here.
- Everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Classes are objects. Class instances are objects. Even modules are objects.
My first thought was, What?. The example given in the book was:
All functions have a built-in attribute
__doc__, which returns the docstring defined in the function’s source code. The
sys module is an object which has (among other things) an attribute called path.
You may have heard the term “first-class object” in other programming contexts. In Python, functions are first-class objects. You can pass a function as an argument to another function. Modules are first-class objects. You can pass an entire module as an argument to a function. Classes are first-class objects, and individual instances of a class are also first-class objects.
- Every function deserves a decent docstring.
- All names in Python are case-sensitive: variable names, function names, class names, module names, exception names. If you can get it, set it, call it, construct it, import it, or raise it, it’s case-sensitive.
- The below statement is like ternary operator in C (?:)
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
- In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.
- Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. This means the argument is optional;
- You can also pass values into a function by name. These are received as key-word arguments. If you pass just a value they are recieved as positional arguments. Positional arguments should be passed in the order. We should pass them first and then the named arguments. Reading the argument list from left to right, once you have a single named argument, the rest of the arguments must also be named.
- i = 10, In python, ‘i’ is a
name with a
binding to the
object created by interger 10. In C langauge, ‘i’ is pointing to a memeory location where the integer value 10 is stored. C developers has to keep this point in mind.