Memory, print(), module, import, keyword
Memory Management in Python -
Whenever program execute, it is copied from hard disc to RAM.
Program is getting executed by computer (Hardware -- OS -- PVM), whenever a program is loaded to RAM, Python virtual machine runs along with OS till program life. For program to execute two logical spaces are allocated (Namespace stack and private heap space).
In program, memory is reserved for variables and objects. Reference variables have name, Object have no name.
Reference variables get memory in Namespace stack.
Objects get memory in private heap space.
Here X is a reference variable and point to id of int object.
X can hold any type of object, and the same reference variable can point to different objects. PVM has a program called memory manager and Garbage collector which is responsible to remove the object which are not referred. Memory manager can call GC as per the requirement, but this is not known to programmer.
These objects for built-ins such as int,float,str are immutable and cannot be changed, so the address of the object remains same and reassigning same object would result earlier address.
Because int object is immutable.
Example - If x is having address of int object that contains 5. Then adding 2 to x doesn't result any changes to the object containing 5, but new object is created and 7 is stored in it.
x = 5
x = x + 2
If object is not referred than reference count is check for that particular block by garbage collector, any object becomes garbage block if its reference count reaches to zero.
Objects and Classes: -
i) Object is Real world entity
ii) Object is an instance of a class
Analogy
Object is a proper noun
Class is a common noun
Teacher can be common noun, but specific teacher is proper noun.
Specific teacher represents example of Teacher or instance of Teacher.
Why Real world?
Because teacher is a category, but Saurav Sir is exact instance which is real world entity.
We can say class works as a template or blueprint to create object.
So, architecture map can be a template/ blueprint, and actual houses constructed using it can be objects. Object is implementation and class is a prototype or description.
Reference variable is created to represent object. Name in a namespace is a variable which is used to contain id of below
i) Instance Object - Object of class (instance) example 5 is instance of int class
ii) Function Object - Object of function type
iii) Class Object
In python, everything is Object.
Here, for function object, print is the reference variable which is having address of function object which has line of code to print input. To prove this concept.
Let's do this -> assign the address which is there in print variable to some other variable, lets say 'a' . Now a will point to function object having logic to print the content. We can use the same reference variable by enclosing parenthesis to print content. So, the code of printing would now become accessible using A reference rather than print. Difference is print reference is assigned by python and it a standard to follow.
If we think in terms of C/C++ , C/C++ provides both SMA and DMA way to create a variable but in python we can only create objects and use with the help of reference variables like in DMA. Due to similar implementation, python tends to be slower than C/C++.
In print() function, by default separator is space, if we want to configure something different then space='required separator has to be given'.
print(24,'March',1988,sep='-'). Whenever in function call, we have assignment operator then that is keyword argument and rest others are positional arguments. Here, sep= is a keyword.
Similarly, by default terminator is '\n' (newline) and separator is ' '(space).
print(24,'March',1988,sep='$',end='#')
>>> 24$March$1988#>>>
Both statements are equivalent
print(''a=',a,'b=',b)
print('a=%d b =%d '%(a,b)) ==> C style print only deference is % instead of ,
Library, module & package
In Python program, we can create instance object, class objects or function object. There would be reference variable which would be holding id. This .py file is called module.
A package is collection of modules and library is collection of packages(folders).
Definitions:
Module: It is a python program file. It can contain instance objects, function objects and class objects. In simple words, it contains variables, functions and classes.
Package: Package is a collection of modules and sub-packages.
Library: Library is a collection of packages.
Import
We can import modules from different location and use it in our program using import
Example -
import keyword
import keyword
keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
Highligted keywords False , None and True are special one which works as a data.
In Python no datatype is keyword like in C/C++, all datatypes are created from class. ***
There is no datatype called char and double
int
float
str
Bool
complex
Comments
Post a Comment