Python Programming

Python Programming

Introduction

  • Instructor: Dr. Shounak Roychowdhury
  • Institution: School of Information, University of Texas at Austin.

Last Session Review

  • Discussed how to handle error scenarios.
  • Reviewed concepts of testing and debugging.

Today's Agenda

  • Review of abstraction and specification.
  • Introduction to Object-Oriented Programming (OOP).
  • Explanation of classes and instances.
  • Brief overview of UML (Unified Modeling Language) and class diagrams.
  • Introduction to Association, Aggregation, Composition, and Dependency in object-oriented design.
  • Discussion of built-in functions in Python.

Abstraction

  • Concept of abstraction in programming:
    • Functions support abstraction by localizing code for better modularization.
    • Abstraction separates definition from invocation, aiding in readability, debugging, and maintenance.
    • Abstraction by parameterization: Function parameters act as abstractions to provide common pathways for data movement, which allows generalization of functional behavior across varied scenarios.
    • Abstraction by specification: Disassociates implementation details from behaviors that users can depend on, enabling independent module changes.
Types of Abstraction
  1. Procedure Abstraction: Introduces new operations using procedures.
  2. Data Abstraction: Introduces new types of data objects.
  3. Iteration Abstraction: Allows iteration over items without exposing details of item retrieval.
  4. Type Hierarchy: Abstracts individual data types into families of related types.

Function Specification

  • Essential questions:
    • How do we know software correctness?
    • Testing with cases ensures functions yield expected results.
    • Complexity increases doubts about test case sufficiency.
Definition of Function Specification
  1. Valid inputs can be described as a set of predicates (preconditions).
  2. Outputs or behaviors based on inputs are described (postconditions).
  3. Correctness of function implementation: Satisfied when all inputs meeting the preconditions return outputs that satisfy the postconditions.
Example of Function Specification
  • Sort specification:
    • ext{val sort: ('a -> 'a -> int) -> 'a list -> 'a list}
    • Function sorts a list in increasing order based on a comparison function which:
    • Returns 0 if arguments are equal,
    • Returns a positive integer if the first argument is greater,
    • Returns a negative integer if the first is smaller.
    • Specification claims guaranteed performance in constant heap space and logarithmic stack space.

Specification

  • A contract between abstraction implementers and clients, clarifying behaviors and responsibilities:
    • Descriptive: Clearly outline the behavior of the abstraction.
    • Responsibility-Clarifying: Establishes whom to hold accountable.
Challenges in Writing Good Specifications
  • Specifications must be:
    • Restrictive enough to rule out irrelevant implementations.
    • General enough to allow useful implementations.
  • Issues often arise from inadequate preconditions or unclear postconditions.
Continuous Specification Updates
  • During Design: Immediately document design decisions.
  • During Implementation: Update specs alongside code revisions; they become obsolete only when the abstraction does.

Abstraction through Specification

  • Precondition: An assertion required to be true when entering a procedure.
  • Effects Assertion: Specifies expected outcomes post invocation for satisfied conditions.
  • Documentation aids in mental modeling, allowing focusing on functionality without delving into implementation.
Benefits of Abstraction by Specification
  1. Locality: Understanding abstraction without implementation examination aids in program development.
  2. Modifiability: Updates can occur without impacting other abstractions, leading to enhanced libraries and performance improvements.

Data Abstraction

  • Facilitates the introduction of new data types by embedding operations within the type's definition. Deferring decisions about data structure implementations until data use is fully understood improves flexibility.

Introduction to Object-Oriented Programming (OOP)

  • Procedural-Oriented Programming (POP): Lists instructions organized into functions; suitable for smaller programs.
  • OOP: Divides programming tasks into objects mixing data (attributes) and functions (methods), forming logical data structures defining real-world entities.
Key Concepts in OOP
  • Inheritance: Creating new classes from existing ones.
  • Encapsulation: Concealing class details from others.
  • Polymorphism: Using common operations in varied ways according to different data types.

Benefits of OOP

  • Clear modular structure enhancing code reusability.
  • Simplifies complex problem-solving.
  • Facilitates abstract data type definitions for real-world modeling, while protecting implementation details, thus demanding clear interfaces.

Abstract Data Types (ADT)

  • Defined by Barbara Liskov, a theoretical concept providing a model for data types as defined by user operations.
ADT Characteristics
TypeProgramming ClarityReusability
PrimitiveLowLow
Structs/TypedefsHighLow
Classes/ObjectsGeneralizedHigh

Classes and Objects

  • Class encapsulates attributes and procedures acting upon data, forming blueprints for object creation.
  • Instantiation creates unique object instances based on defined classes.
Instantiation in Python
  • Special method init() customizes object state upon creation. Arguments passed during instantiation are handled within this method.
  • Example with Complex class demonstrating instantiation.
Example of Using Classes
  • Calculator Class: Manages an initial memory, allowing operations like add/subtract while fetching memory values.
  • Room Class: Enables area calculations through specified dimensions.
  • House Class: Manages collections of rooms to compute the aggregate area.
Working with Instances of Classes
  • Illustrates the differences in attributes and methods between different object instances.

Understanding UML (Unified Modeling Language)

  • A visual language designed to diagram relationships between classes for easier comprehension among software engineers and business analysts.

Diagrams Associated with UML

  1. Structure Diagrams: Class Diagrams, Object Diagrams, etc.
  2. Behavior Diagrams: Use Case Diagrams, Activity Diagrams, etc.
  3. Interaction Diagrams: Sequence Diagrams, Communication Diagrams, etc.
Example of UML Class Diagram
  • Online Shopping system structure modeling user interactions and order management, including detailed attributes and states of various classes.

Class Relationships

  • Association: General relationships between classes indicating object references.
  • Aggregation: A one-way relationship where entities can exist independently.
  • Composition: A strong dependency between entities, implying that one cannot exist without the other.
  • Dependency: Indicates changes in one class affect another.
Examples of Class Relationships in Practice
  • Demonstrates relationships through Python code examples implementing product management with interaction between product locations.

Built-in Functions and Attributes

  • Python provides numerous built-in functions and attributes facilitating class interaction and development.

Dunder Methods

  • Dunder methods (double underscore methods) allow customization of class behavior with common operations in Python, including arithmetic, object creation, destruction, string representation, etc.