Functions |
Objects in Python Life & Death on Heap |
Call Stack |
Stack vs Heap Memory |
Functions |
Garbage Collection |
Problems |
What happens when you write?
Text
x = 10
y = 10
print(id(x))
print(id(y))
Both 'x' and 'y' refer to same object.
Text
x = 10
y = 10
print(id(x))
print(id(y))
A Function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
def sayHello():
print("hello world")
sayHello()
Data can be passed to functions using one or more parameters. Parameters can have default values.
Why Create Methods.
Write a method to find absolute value of a number.
Write methods to convert
- decimal number to binary number.
- binary number to a decimal number
Objects live on the Heap!
Garbage Collection deals with finding and deleting the garbage from memory.
However, in reality, Garbage Collection tracks each and every object available in the heap space and removes unused ones.
In Java, GC works in two simple steps known as Mark and Sweep:
Mark – it is where the garbage collector identifies which pieces of memory are in use and which are not
Sweep – this step removes objects identified during the “mark” phase
In Python, GC works by reference counting algorithm. Memory is freed up when the reference count becomes zero for that object.
Advantages
Disadvantages