python

39
PYTHON Volkan Tüfekçi – Gülcan Aydın 26.12.2007

Upload: volkant

Post on 19-May-2015

2.750 views

Category:

Business


0 download

DESCRIPTION

Python's history, philosophy. Lists, dicts, tuples. OOP in python, inheritance, data abstraction, polymorphism.

TRANSCRIPT

Page 1: Python

PYTHON

Volkan Tüfekçi – Gülcan Aydın26.12.2007

Page 2: Python

History Philosphy UsageGuido van Rossum 1989 Hobby LanguageABC – Modula 3 – Unix/C – Common Lisp - HaskellDislikes:

Difficulty of adding new "primitive" operationsMonolithic, "closed system“, basic I/O operationsIdeosyncratic syntax (all uppercase keywords!)

Aims:ReadabilityRely on the Unix infrastructure and conventions,

without being Unix-boundImportance of programmer effort over computer

effort

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 2

Page 3: Python

History Philosphy Usage(cont.)1991Syntax and semantics are minimalist, while the

standard library is large and comprehensivemulti-paradigm programming language:

object orientation and structured programming -> fully functional programming and aspect-oriented

programming -> by language featurespyDBC and Contracts for Python which allow Design by

Contractdynamic name resolution (late binding)New built-in modules are easily written in C or C++Python philosophy rejects exuberant syntax, such as in PerlPyQt, PyGTK, PyCon, PyPy

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 3

Page 4: Python

History Philosphy Usage(cont.)Nasa, Google, YouTube, BitTorrent, Air

Canada’s Reservation ManagementTurboGears, Django(NewYork Times’ web

site)Maya, BlenderFedora, Gentoo, PardusJython, IronPython

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 4

Page 5: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 5

Introduction to O-O PythonPython is an object-oriented programming language. Supports procedural programming with modules and

functions. Generally, the O-O paradigm is suitable when you

want to group state (data) and behavior (code) together in handy packets of functionality.

The procedural paradigm, based on modules and functions, tends to be simpler and is more suitable when you don't need any of the benefits of object-oriented programming.

Page 6: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 6

Syntax All values in Python can be used as logic values. Some of the more

“empty” ones, like [], 0, “” and None represent logical falsity, while most other values (like [0], 1 or “Hello, world”) represent logical truth.

if 0 < month <= 12:doSomething()

Page 7: Python

Dictionaries 1

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 7

• A dictionary in Python is like an instance of the Hashtable class in Java• No duplicate member• No order• Mutable

Page 8: Python

Dictionaries 2

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 8

Page 9: Python

Lists 1

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 9

Page 10: Python

Lists 2

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 10

Page 11: Python

Lists 3

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 11

Page 12: Python

List & Dictionary Examples

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 12

L=[1,2,3,4,5] IMPORTANT: The value will be stored into a range of memory blocks, and what if we do

this? L2=L

It make L2 refers to the same memory blocks where L points to. Example;

Page 13: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 13

List Comprehensions

Page 14: Python

Tuples

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 14

• A tuple is an immutable list. A tuple can not be changed in any way once it is created• Tuples are faster than lists• It makes your code safer if you “write-protect” data• Tuples can be converted into lists, and vice-versa.

Page 15: Python

Assigning Variables

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 15

Page 16: Python

String Formatting & Concatenating

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 16

# 1 membered Tuple!!!

# Strong typed

Page 17: Python

Importing Modules & Getting Help

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 17

Page 18: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 18

String Operation Examples

Page 19: Python

Modulesfrom UserDict import UserDict

Page 20: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 20

ConstructorA constructor is a method that initializes a newly

created object.The overridden derived-class constructor usually

calls the base-class constructor, to initialize base-class attributes before initializing derived-class attributes.

If a derived class does not define a constructor, the class’s base-class constructor executes when the client creates a new object of the class.

Page 21: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 21

Destructor & Static Variable

Does not actually destroy the object

# Static variable

# 0

# 2

# 0

Page 22: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 22

Static Methods

Page 23: Python

Property Descriptor

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 23

Page 24: Python

Slots Attribute

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 24

>>>8

Page 25: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 25

InheritanceInheritance is a form of software reusability in which

new classes are created from existing classes by absorbing their attributes and behaviors.The new class is referred to as a derived class, inherit

from the class referred to as base class.With single inheritance,a class is derived from one

base class.With multiple inheritance, a derived class inherits from

several base classes.

Page 26: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 26

(cont.)We distinguish between “is-a” relationships and

“has-a” relationships:“Is a” is inheritance. In an “is a” relationship, an object

of a derived-class type may also be treated as an object of the base-class type.

“Has a” is composition. In a “has a” relationship, an object has references to one or more objects of other classes as members.

Page 27: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 27

“is a”Inheritance hierarchy for university community members :

Page 28: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 28

Multiple Inheritance and Name Clash Problem

class Base1: def amethod(self): print "Base1“

class Base2(Base1): pass # pass!!!class Base3:

def amethod(self): print "Base3"

class Derived(Base2, Base3): pass aninstance = Derived( ) aninstance.amethod( ) # prints: "Base1"

Page 29: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 29

Data AbstractionOne of the fundamental principles of good software

engineering is that a client should not need toknow how a class is implemented to use that class.

Python’s use of modules facilitates this dataabstraction—a program can import a class definition and use the class without knowing how theclass is implemented.

Page 30: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 30

EncapsulationNo methods or member variables are protected (or

private or the like) in Python. Encapsulation is pretty much a matter of

programming style. If you really need it, there are naming-conventions that

will allow some privacy. such as; _ _ident Python compiler implicitly changes the identifier into

_classname_ _ident, where classname is the name of the class.

Page 31: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 31

Example of Private Data Access

Page 32: Python

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 32

Abstract Classes

Traceback (most recent call last): b.earnings() raise NotImplementedError, "Cannot call abstract method"NotImplementedError: Cannot call abstract method

Page 33: Python

Polymorphism

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 33

Page 34: Python

Polymorphism 2

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 34

Page 35: Python

Polymorphism 3

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 35

Page 36: Python

Polymorphism 4

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 36

Page 37: Python

Polymorphism 5

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 37

Page 38: Python

Composition vs. Inheritance

composition of existing classesEmployee is a BirthDate or that an

Employee is a TelephoneNumberEmployee has a BirthDate and that an

Employee has a TelephoneNumber.

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 38

Page 39: Python

“Uses A” and “Knows A” Relationshipsa person object is not a car and a person

object does not contain a car, a person object certainly uses a car

A program uses an object simply by calling a method of that object through a reference.

one object is said to have a knows a relationship with the other object; this is sometimes called an association.

12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 39