About Dynamic Languages and Python

Author: Jeff Rush
Date: 2006-10-05

A walk through programming wonderland...

Overview

  • definitions, definitions, definitions...
  • programming paradigms and methodologies
  • data typing (dynamic, static, weak, strong)
  • "scripting" languages
  • compilation versus interpretation
  • introspection and reflection

What is a Dynamic Language?

  • a very high-level language e.g. handles memory management for you
  • can add new code at runtime
  • can extend objects and definitions while running
  • can query and modify the type system
  • supports introspection and reflection
  • continuations
  • often supports closures

What is a Dynamic Language Not?

  • a dynamic language != dynamically typed
  • an object orientation is not required e.g. Perl
  • can still be compiled (into bytecode) and yet be interpreted

Reflection

  • process by which a program can examine and modify its structure and behavior while executing
  • compile-time or static reflection exists but is uncommon e.g. C++ RTTI
  • functions, classes and such are 1st-class entities
  • doc strings for Python

1st-Class vs 2nd-Class Entities

  • functions in C/C++ are not 1st-class entities - cannot create new functions at runtime
  • classes in C++ are not 1st-class entities - cannot create new classes at runtime - can create other types at runtime
  • second-class means a lesser degree of manipulation
  • beyond that means no degree of runtime manipulation

Type Introspection

  • ability to query the type of an object at runtime
  • common in any language with classes as 1st-class objects
  • introspection easier when compile/link steps merged

Type Systems

  • two completely unrelated dimensions
  dynamic static
strong Python, Ruby Java, FORTRAN
weak Perl, PHP COBOL, C
  • whether variables change their type, once set
  • whether type mismatches are implicitly fixed

Static Typing

  • respects distinction btw runtime and compile-time
  • variables don't change their types at runtime
  • paths of execution can be optimized
  • common in "compiled" languages
  • metaclasses, introspection are more difficult

Dynamic Typing

  • blends the distinction btw runtime and compile-time
  • makes decision of what to call at runtime
  • variables acquire different types depending on execution path
  • common in "scripting languages", interpreted languages
  • makes metaprogramming more powerful and easier to use
  • harder to optimize execution paths

Duck Typing - a Form of Dynamic Typing

  • if it acts like a duck, walks like a duck
  • languages "guess" the type of a value
  • avoid need for a rigid class hierarchy
  • when formalized becomes "interface typing"
  • (file manager example)

Strong Typing

  • counter to weak typing
  • prevents operations in which arguments have the wrong type e.g. trying to add a numeric string to an integer fails
  • can hurt reusability

Weak Typing

  • counter to strong typing
  • implicit conversion/casting of types
  • can hide problems

Continuations

  • a representation of the executation state of a program - the call stack - values of selected variables
  • ability to continue execution at a later time
  • useful for: - algorithms that generate data which is not all used - lightweight threading - checkpointed or mobile algorithms

Closures

  • a function that refers to free variables in its lexical context
  • sometimes better alternatives to objects
  • refers to a linkage between contexts in a higher dimension
  • a way of providing plug-in points in an algorithm - a compare function passed into a sort operation - except a closure has better lexical access

Paradigms versus Methodologies

  • a paradigm (thought pattern) - is an algorithm for computers
  • a methodology (codified set of practices) - is an algorithm for people

Methodologies

  • flowcharting
  • structured programming
  • top-down programming
  • object-oriented programming
  • extreme programming
  • is a paradigmatic style of software engineering

Paradigms

  • procedural - a list of instructions + subroutines
  • functional
  • object-oriented
  • declarative
  • determines the view that the programmer has of the execution of the program

Imperative Programming

  • emphasizes - changes in state - execution of sequential commands
  • also called procedural programming
  • led to methodology of structured programming

Declarative Programming

  • in contrast to 'imperative programming'
  • describes 'what something is like' rather than 'how to create it'
  • HTML and SQL are declarative languages

Object-Oriented Programming

  • a collection of units of objects that act on each other
  • such units consist of data (scattered across various memory spaces) and behavior/code (stored in the class and shared by all units)

Object-Based Programming

  • a subset of object-oriented programming e.g. Visual Basic lacks impl inheritance or JavaScript that uses prototypes not clases
  • lacks inheritance
  • lacks per-instance data storage
  • led to the methologies: - design patterns - design by contract - modeling languages

Functional Programming

  • views computation as the evaluation of math functions
  • avoids state and mutable data
  • used in functional, imperative and object programming

Object-Oriented Languages - Simula 67

  • first language to use objects as programming entities
  • but such were static - non-instanced (data was per-class) - no use of inheritance.

Object-Oriented Languages - Smalltalk

  • 1970's
  • introduced the term "object oriented programming"
  • object could be created, modified and consumed on the fly

Object-Oriented Languages - Oberon

  • a distinctive approach unlike Smalltalk
  • an approach very unlike C++

What is a Scripting Language?

  • created to shorten the edit-compile-link-run process
  • early instances were batch languages or job control languages
  • some usage areas: - system administration - gui component assembly - application behavior manipulation

Properties of a Scripting Language

  • automated memory management
  • bounds checking
  • a high to very high conceptual level
  • can be interpreted or compiled
  • often has an interactive prompt
  • inheritance; single or multiple; method resolution order

Choosing a (Dynamic) Language

In choosing a language, consider

  • ease of expression - statements ratio: - C=1, C++=2.5, Perl=6, Python=6
  • ease of handing off to someone else
  • richness of libraries
  • quality of documentation - books, articles
  • availability of workers/vendors
  • license
  • community - size - friendliness - focus, slant, skill level
  • packaging technology and distribution system

About Python - The Language

  • 1991
  • a fully dynamic language
  • strongly typed (no implicit conversions)
  • dynamically typed (can changes types at runtime)
  • multiple inheritance
  • multi-paradigm - procedural - object-oriented - functional
  • garbage collection - ref counting
  • "remarkable power, clear syntax"

About Python - The Library

  • extensive standard libraries
  • has a packaging format (.egg)
  • has a distribution repository (cheeseshop.python.org)
  • operates within many different problem domains - scientific - web, ftp, network - graphical
  • integrates with other languages and tools

About Python - The Social Aspects

  • simple syntax; can be learned in a few days
  • readable, maintainable
  • interactive prompt
  • btw APL and COBOL on the wordiness scale
  • tends to use keywords rather than punctuation
  • national conferences (3/year)
  • active mailing list, blogs, IMs

Python is Not an Either-Or Language

  • multiplatform
  • multiple virtual machines - PYC - JVM - CLR
  • Java world - Jython - full access to Java libraries - Eclipse drop-in
  • .NET world - IronPython - full access to .NET framework - Visual Studio drop-in

Questions?

  • ???