python
DESCRIPTION
Python's history, philosophy. Lists, dicts, tuples. OOP in python, inheritance, data abstraction, polymorphism.TRANSCRIPT
PYTHON
Volkan Tüfekçi – Gülcan Aydın26.12.2007
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
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
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
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.
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()
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
Dictionaries 2
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 8
Lists 1
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 9
Lists 2
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 10
Lists 3
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 11
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;
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 13
List Comprehensions
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.
Assigning Variables
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 15
String Formatting & Concatenating
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 16
# 1 membered Tuple!!!
# Strong typed
Importing Modules & Getting Help
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 17
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 18
String Operation Examples
Modulesfrom UserDict import UserDict
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.
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
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 22
Static Methods
Property Descriptor
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 23
Slots Attribute
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 24
>>>8
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.
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.
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 27
“is a”Inheritance hierarchy for university community members :
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"
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.
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.
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 31
Example of Private Data Access
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
Polymorphism
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 33
Polymorphism 2
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 34
Polymorphism 3
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 35
Polymorphism 4
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 36
Polymorphism 5
12.04.23CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN 37
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
“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