the awesome python class part-4

12
The Awesome Modules, Functions, Class, Metaclass, OOPs Concepts. Part-4

Upload: binay-kumar-ray

Post on 14-Apr-2017

213 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: The Awesome Python Class Part-4

The Awesome

Modules, Functions, Class, Metaclass, OOPs Concepts.

Part-4

Page 2: The Awesome Python Class Part-4

Summary of Previous Parts

We are done with discussing the python Installation, Environment setup, Data types in python, Basic Data structures, their syntaxes and their usage.

In the last presentation we have covered inputs, Iterators & generators, Range & Xrange, Operators, Control flow, Branches, Expression and Order of Operation.

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

2

Page 3: The Awesome Python Class Part-4

Modules

In python a module is something that has one or many files and has atleast one __init__.py file.

__init__.py is usualy a blank file, but the interpreter defines a directory as a module if the file is present inside the directory.

User has the power to override the init by allowing only those methods he/she wants to allow.

3

Page 4: The Awesome Python Class Part-4

Functions

If you know any programming language, you should probably know functions very well. However If you don’t, function is something that has a specific set of code for execution of some action.

It may or may not have a return statement.

In python a typical function is defined with the help of keyword ‘def’.

Syntax:

4

Page 5: The Awesome Python Class Part-4

Function Example

Here is a function to print all integers less than 100.

If you call the above method just by typing print_int_lt_100() it prints all integers less than 100.

5

Page 6: The Awesome Python Class Part-4

Class

Think of a class as a blueprint. It isn't something in itself, it simply describes how to make something. You can create lots of objects from that blueprint - known technically as an instance.

It is a mixture of the class mechanisms found in C++ and Modula-3.

By default class members are public.

Syntax:

6

Page 7: The Awesome Python Class Part-4

MetaclassesLike i said Everything in python is an object, the class definition is also an object.

Does that mean instance of any class in python is an object of object?Yes, It is.

A metaclass is the class of a class. Like a class defines how an instance of the class behaves, a metaclass defines how a class behaves. A class is an instance of a metaclass.

7

Page 8: The Awesome Python Class Part-4

Object Oriented Python In OOP programmers define not

only the datatype of a data structure, but also the types of operations (functions) that can be applied to the data structure.

8

Oops Concepts like inheritance, polymorphism

etc.

Page 9: The Awesome Python Class Part-4

Terms

class variable

data member

self

__init__

__del__

static method

class method

inheritance

function overloading

Operator overloading

abstract classes

metaclasses

9

Page 10: The Awesome Python Class Part-4

Class Members, self, constructors and destructors

Forget about class members, data members etc and think about an UNIT.

This UNIT has it’s attributes, methods and what not.

A perfect example for class in real time is an UNIT in an organisation.

Or a real time Person Class.

Person can have age, name etc and the person can take various actions.

Then there is something called self.

There needs to be something that connects within the unit that validates the data member and methods.

There needs to be something or some power that creates the instance and assigns values.

There needs to be something that destroys the instance.

10

Page 11: The Awesome Python Class Part-4

Old Style vs New Style Python Classes

Old Style classes does not inherit from any other class.

Old style classes are not there anymore in python 3.x

Old style classes are good.

1. New style classes inherits from object or other new style classes.

2. New style classes are The Thing now with many new features, changed MRO standards and many more.

3. New style classes are better.

11

Page 12: The Awesome Python Class Part-4

ThanksStay Tuned for the next part. Enjoy !!

12