The Awesome Python Class Part-5

12
The Awesome File handling, Exceptions, Standard libraries, debugger, magic functions, lambda forms, assert, decorators, memory management. Part-5

Transcript of The Awesome Python Class Part-5

Page 1: The Awesome Python Class Part-5

The Awesome

File handling, Exceptions, Standard libraries, debugger, magic functions, lambda forms, assert, decorators, memory management.

Part-5

Page 2: The Awesome Python Class Part-5

Summary of Previous Parts

We are done with discussing the Data types in python, Basic Data structures, their syntaxes and their usage, inputs, Iterators/generators, Operators, Control flow, Branches etc.

In the last presentation we have covered, Modules, Functions, Class, Metaclass, OOPs Concepts.

We will move forward with lot many things, I hope you enjoy the show.

2

Page 3: The Awesome Python Class Part-5

File Handling

3 different modes in file handling. ‘r’, ‘w’, ‘a’.

‘b’ is file handling mode for binary, that you can append with any type and change at binary level.

Open has to be followed by close.

with open doesn’t need any close.

Be careful if file is opened in write mode it removes everything that is already present.

3

Page 4: The Awesome Python Class Part-5

Exceptions

Syntax

4

● One try can have multiple excepts.

● Try can have else which will get executed when no exception occurs.

● finally can be used with try which will always get executed irrespective of any situation.

● We can manually raise exception if we want.

Page 5: The Awesome Python Class Part-5

Standard Libraries

There is a hell lot of standard libraries present in python.

Refer the link to read about all standard libraries.https://docs.python.org/2/library/

5

Page 6: The Awesome Python Class Part-5

Debugger & Magic Functions

Python debugger is a very useful and important tool in development.

There are many functions present in debugger.

set_trace is the main function to debug the code flow.

6

Magic functions are special methods that you can define to add "magic" to your classes.

They're always surrounded by double underscores (e.g. __init__ or __lt__).

They're also not as well documented as they need to be.

All of the magic methods for Python appear in the same section in the Python docs, but they're scattered about and only loosely organized.

Page 7: The Awesome Python Class Part-5

LambdaThese functions are throw-away functions,

i.e. they are just needed where they have been created.

Lambda functions are mainly used in combination with the functions filter(), map() and reduce().

The lambda feature was added to Python due to the demand from Lisp programmers

7

An anonymous functions

Page 8: The Awesome Python Class Part-5

Map, Reduce, FilterUsing map and data set can be

mapped to a function like below.

Syntax is map(aFunction, aSequence) 8

Reduce is like map but it uses lambda to reduce the data to a value instead of mapping.

Page 9: The Awesome Python Class Part-5

assert

Assert is always True.

Any value other than True will break the assert.

9

Page 10: The Awesome Python Class Part-5

Decorators

Decorators allow you to make simple modifications to callable objects like functions, methods, or classes. We shall deal with functions for this tutorial.

10

Page 11: The Awesome Python Class Part-5

Memory Management

Memory management in Python involves a private heap containing all Python objects and data structures. Interpreter takes care of Python heap and that the programmer has no access to it.

The allocation of heap space for Python objects is done by Python memory manager. The core API of Python provides some tools for the programmer to code reliable and more robust program.

Python also has a build-in garbage collector which recycles all the unused memory. When an object is no longer referenced by the program, the heap space it occupies can be freed.

The garbage collector determines objects which are no longer referenced by the sprogram frees the occupied memory and make it available to the heap space.

The gc module defines functions to enable /disable garbage collector:gc.enable() -Enables automatic garbage collection.gc.disable() - Disables automatic garbage collection.

11

Page 12: The Awesome Python Class Part-5

ThanksStay Tuned for the next part. Enjoy !!

12