Lets code classes_python

19
Presented by: Prithwish Chakraborty LET’S CODE!!! Sponsored by: AKAMAI Python : Object Oriented Programming Reference: http ://www.tutorialspoint.com/python/python_classes_objects.htm

description

Classes in python

Transcript of Lets code classes_python

Page 1: Lets code classes_python

Presented by: Prithwish Chakraborty

LET’S CODE!!!

Sponsored by: AKAMAI

Python : Object Oriented Programming

Reference: http://www.tutorialspoint.com/python/python_classes_objects.htm

Page 2: Lets code classes_python

2Let’s Code: Session 3

PythonVariables:Basic data structures :

ListsDictionariesTuples

Basic operators: Addition/subtractionDivision/Mutliplication

Functions??

3/28/2013

What have we learnt so far??

Page 3: Lets code classes_python

3Let’s Code: Session 3 3/28/2013

Recap of function syntax

Page 4: Lets code classes_python

4Let’s Code: Session 3

Problem: Build an employee database

3/28/2013

Let’s do something

Page 5: Lets code classes_python

5Let’s Code: Session 3

Problem: Build an employee database

What attributes do we want to keep? Employee Name: e.g. Sylvester StalloneEmployee Address: e.g. (Beverly Hills, California,

USA)Employee Id: e.g. 127001Employee Salary: e.g. $1000000

What operations do we want?What other things can we do?

3/28/2013

Let’s do something

Page 6: Lets code classes_python

6Let’s Code: Session 3

Problem: Build an employee database

What attributes do we want to keep?What operations do we want?

Get Employee NameGet Employee idChange Salary

Give 100$ bonus if address is in Beverly HillsAdd a new information about the employee

Add info about speciality e.g. “Mind Blowing Action Movies”What else can we do?

3/28/2013

Let’s do something

Page 7: Lets code classes_python

7Let’s Code: Session 3

Create an Employee:Which data structure to use ??

Lets use dict

Attributes:

3/28/2013

Let’s start off!!

Attribute Type Example

name string “Sylvester Stallone”

address tuple (“Beverly Hills”,”CA”,”USA”)

id int 127001

salary int 1000000

Page 8: Lets code classes_python

8Let’s Code: Session 3

Create an Employee:Functions:

Now let us extend this to a database >Non-object oriented wayObject Oriented way

3/28/2013

Let’s start off!!

Actions Example Logic

get_employee_name

“Sylvester Stallone”

get_employee_id 127001

change_salary if(address[0] == “Beverly Hills”): salary += 100

Add_new_info Add attribute: Speciality

Page 9: Lets code classes_python

9Let’s Code: Session 3

Basic philosophyA “container” to hold attributes and operations

realted to those attributes in one single place!!

Anybody having C background?? – lets start of by thinking of struct: container to hold data

How about if we have some funcitons?

What you ask about the syntax?? ok so be it

3/28/2013

OOPS – the world of infinite possibilites

Page 10: Lets code classes_python

10Let’s Code: Session 3 3/28/2013

OOPS – baby stepsExample of a class:

Special notes : self

Page 11: Lets code classes_python

11Let’s Code: Session 3 3/28/2013

OOPS – baby stepsExample of a class:

Special notes : self

Looking a bit deepr: where are these created

Page 12: Lets code classes_python

12Let’s Code: Session 3

Some other conceptsAdding attributes

Deleting instances

Control the delete operation

3/28/2013

OOPS –baby steps

Page 13: Lets code classes_python

13Let’s Code: Session 3

Problem: Build an employee database

What attributes do we want to keep?What operations do we want?What else can we do?

Extend definitons of EmployeeMultiple Inheritance

3/28/2013

Let’s do something

Page 14: Lets code classes_python

14Let’s Code: Session 3

Creating a “super Employee”

Use attributes the same as Employee but add a new attribute: position

Way out : Inheritance

3/28/2013

OOPS – ok time for some more steps

Page 15: Lets code classes_python

15Let’s Code: Session 3

Change the bonus function

Give 100$ more if CEOWay out function overloading

3/28/2013

OOPS – I wanna change the world

Page 16: Lets code classes_python

16Let’s Code: Session 3

Multiple InheritanceAnother “base class”

Creating the super Duper employee

3/28/2013

OOPS – wow every thing is so easy!!

Page 17: Lets code classes_python

17Let’s Code: Session 3

Operator overloading

Iterators

Generators

3/28/2013

Future Things

Page 18: Lets code classes_python

18Let’s Code: Session 3

A Big thanks to the sponsor for this session:

And best of all!! Akamai is currently recruiting for summer interns. All positions are listed at jobs.akamai.com or you can send resumes directly to [email protected]

3/28/2013

Acknowledgement

Page 19: Lets code classes_python

19Let’s Code: Session 3

Sample Codes: https://c9.io/pchakraborty/lets-code

Other refereces: http://docs.python.org/2/tutorial/classes.html

3/28/2013

References