
Quick Contact
Python Tutorial
- What is Python?
- How to Install Python?
- Python Variables and Operators
- Python Loops
- Python Functions
- Python Files
- Python Errors and Exceptions
- Python Packages
- Python Classes and Objects
- Python Strings
- PostgreSQL Data Types
- Python Generators and Decorators
- Python Dictionary
- Python Date and Time
- Python List and Tuples
- Python Multithreading and Synchronization
- Python Modules
- What is Python bytecode?
- Python Regular Expressions
Python Selenium
Interview Questions & Answers
What is Python Bytecode?
Python is a hybrid interpreter. While running a program, it first assembles it into bytecode that can then be run in the Python interpreter, also known as Python virtual machine.
Example
>>> def hello():
… print “Hello, World”
…
>>> dis.dis(hello)
2 0 LOAD_CONST 1 (‘Hello, World’)
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 0 (None)
8 RETURN_VALUE
The Python interpreter is stack-based and utilized a first-in last-out system. Every operation program (opcode) in the Python assembly language (the bytecode) takes a fixed number of elements from the stack and returns a fixed number of elements to the stack. If there are not just items on the stack for an opcode, the Python interpreter will crash, possibly without an error message.
Constants in the dis module
Example
EXTENDED_ARG = 145 # All opcodes greater than this have 2 operands
HAVE_ARGUMENT = 90 # All opcodes greater than this have at least 1 operands
cmp_op = (‘<‘, ‘<=’, ‘==’, ‘!=’, ‘>’, ‘>=’, ‘in’, ‘not in’, ‘is’, ‘is …
# A list of comparator id’s. The indices are used as operands in some opcodes
# All opcodes in these lists have the respective types as there operands
hascompare = [107]
hasconst = [100]
hasfree = [135, 136, 137]
hasfree = [135, 136, 137]
hasjrel = [93, 110, 120, 121, 122, 143]
haslocal = [124, 125, 126]
hasname = [90, 91, 95, 96, 97, 98, 101, 106, 108, 109, 116]
# A map of opcodes to ids
opmap = {‘BINARY_ADD’: 23, ‘BINARY_AND’: 64, ‘BINARY_DIVIDE’: 21, ‘BIN…
# A map of ids to opcodes
opname = [‘STOP_CODE’, ‘POP_TOP’, ‘ROT_TWO’, ‘ROT_THREE’, ‘DUP_TOP’, ‘…
Disassembling modules
To disassemble a Python module, first this has to be turned into a .pyc file (Python compiled). To do this, run
python -m compileall .py
Then in an interpreter, run
import dis
import marshal
with open(“.pyc”, “rb”) as code_f:
code_f.read(8) # Magic number and modification time
code = marshal.load(code_f) # Returns a code object which can be disassembled
dis.dis(code) # Output the disassembly
This will compile a Python module and output the bytecode instructions with dis. The module is never imported so it is safe to use with untrusted code.
The base64 Module
Parameters | Parameters |
---|---|
base64.b64encode(s, altchars=None) s altchars | A bytes-like object. A bytes-like object of length 2+ of characters to replace the ‘+’ and ‘=’ characters when creating the Base64 alphabet. Extra characters are ignored. |
base64.b64decode(s, altchars=None, validate=False) s altchars validate |
A bytes-like object A bytes-like object of length 2+ of characters to restore the ‘+’ and ‘=’ characters when generating the Base64 alphabet. Extra characters are ignored. If validate is True, the characters not in the normal Base64 alphabet or the alternative alphabet are not discarded before the padding check. |
base64.standard_b64encode(s) s |
A bytes-like object |
base64.standard_b64decode(s) s |
A bytes-like object |
base64.urlsafe_b64encode(s) s |
A bytes-like object |
base64.urlsafe_b64decode(s) s |
A bytes-like object |
b32encode(s) s |
A bytes-like object |
b32decode(s) s |
A bytes-like object |
base64.b16encode(s) s |
A bytes-like object |
base64.b16decode(s) s |
A bytes-like object |
base64.a85encode(b, foldspaces=False, wrapcol=0, pad=False, adobe=False) b |
A bytes-like object |
foldspaces | If foldspaces is True, the character ‘y’ will be used instead of 4 consecutive spaces. |
Encoding and Decoding Base64
We include the base64 module in our script, we should import it first:
Example1
import base64
s = “Hello World!”
b = s.encode(“UTF-8”)
The output of the last line would be:
b’Hello World!’
The b prefix is used to denote the value is a bytes object.
To Base64 encode these bytes, we use the base64.b64encode() function:
Example2
import base64
s = “Hello World!”
b = s.encode(“UTF-8”)
e = base64.b64encode(b)
print(e)
Output
When we execute the following commands, it displays the following output.
b’SGVsbG8gV29ybGQh’
which is still in the bytes object. To get a string out of these bytes, we can use Python’s decode() method with the UTF-8 encoding:
import base64
s = “Hello World!”
b = s.encode(“UTF-8”)
e = base64.b64encode(b)
s1 = e.decode(“UTF-8”) print(s1)
Output
SGVsbG8gV29ybGQh
If we wanted to encode the string and then decode we could use the base64.b64decode() method:
import base64
# Creating a string
s = “Hello World!”
# Encoding the string into bytes
b = s.encode(“UTF-8”)
# Base64 Encode the bytes
e = base64.b64encode(b)
# Decoding the Base64 bytes to string
s1 = e.decode(“UTF-8”)
# Printing Base64 encoded string
print(“Base64 Encoded:”, s1)
# Encoding the Base64 encoded string into bytes
b1 = s1.encode(“UTF-8”)
# Decoding the Base64 bytes
d = base64.b64decode(b1)
# Decoding the bytes to string
s2 = d.decode(“UTF-8”)
print(s2)
Output
When we execute the following command it displays the following output.
Base64 Encoded: SGVsbG8gV29ybGQh
Hello World!
Enroll Yourself: Python Training In Delhi