
From "Python Tutorial" by Guido van Rossum, section 3.1.1
From "Python Tutorial" by Guido van Rossum, section 3.1.2
From "Python Tutorial" by Guido van Rossum, section 3.1.4
From "Python Tutorial" by Guido van Rossum, section 5.5
>>> l = {'Python': ':-)', 'Prolog': ':-('}
>>> l['Java']
Traceback (most recent call last):
...
KeyError: 'Java'
>>> l.get('Java',':-|')
':-|'
>>> l.setdefault('Java',':-|')
':-|'
>>> l
{'Python': ':-)', 'Java': ':-|', 'Prolog': ':-('}
Python is strongly but dynamically typed.
>>> var = 5 >>> 5/2 2 >>> 5 + 'test' Traceback (most recent call last): ... TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> var = 'string' >>> var + 'test' 'stringtest' >>> var/2 Traceback (most recent call last): ... TypeError: unsupported operand type(s) for /: 'str' and 'int'
From "Python Tutorial" by Guido van Rossum, section 4.1
From "Python Tutorial" by Guido van Rossum, section 3.2
From "Python Tutorial" by Guido van Rossum, section 4.2 and section 4.3
return None #Python's "None" type works wellraise Exception, "Operation Failed"with block.
with open('foo.txt') as f:
...Pattern 1.
>>> try: ... raise Exception, 'Error' ... except Exception, e: ... print e ... raise ... else: ... print 'No Error' ... Error Traceback (most recent call last): ... Exception: Error
Pattern 2.
>>> try: ... raise Exception, 'Error' ... finally: ... print 'Cleaning Up' ... Cleaning Up Traceback (most recent call last): ... Exception: Error
In Python 2.5, you can also add the
finally clause to Pattern 1.
with blocks.with wrappers.from __future__ import with_statementFirst, let's try building objects by hand. This is what you would do with a language such as C -- which has no explicit Object-Oriented features.
class Something(object): pass #Ignore this for now! pen = Something() pen.is_open = True #Add an attribute print pen.is_open
Objects also have behavior, not just attributes.
def write(self, data):
if not self.is_open:
raise Exception, 'Open the pen before writing'
print data
write(pen, 'Hello')
pen.is_open = False
write(pen, 'Hello')
This illustrates Python's ability to build arbitrary objects at run time.
import types
pen.write = types.MethodType(write, pen)
pen.write('Hello')
Is this a pen ... or a duck?
def say(self):
if self.is_lame:
return 'Help!'
else:
return 'Quack!'
pen.is_lame = False
pen.say = types.MethodType(say, pen)
print pen.say()
Python is strongly but dynamically typed. This allows a very direct approach to polymorphism.
Polymorphism is the most important aspect of Object-Oriented Development
def make_speak(speaker): #Note: just a plain function, not a method
print speaker.say() #The "speaker" can be anything -- as long as it has
# a "say" interface
make_speak(pen)
Constructing objects directly is great for special cases, but is too tedious for everyday programming tasks. What if you need several "pen" objects, for example? We need a meta-object to describe pens. Such a meta-object is called a Class.
class Pen(object):
def __init__(self):
self.is_open = True
def write(self, data):
if not self.is_open:
raise Exception, 'Open the pen before writing'
print data
another_pen = Pen()
another_pen.write('Hello, Again')
Just like Python allows you to effectively modify an object's type at run time, you can similarly change a class at run time.
Pen.say = say another_pen.is_lame = False print another_pen.say()
Of course, you can also generate entire classes at run time.
Duck = type('Duck', (), {'is_lame':False, 'say':say})
duck = Duck()
print duck.say()
Goose is a Duck.
class Goose(Duck):
def say(self):
return Duck.say(self)+' Honk!'
goose = Goose()
print goose.say()
GooseDelegator has a Duck (or something else, as long as the interface is correct).
class GooseDelegator(object):
def __init__(self, delegate):
self._delegate = delegate
def say(self):
return self._delegate.say()+' Honk!'
dgoose = GooseDelegator(Duck())
print dgoose.say()
__metaclass__
attribute.
class MetaClassLogger(type):
def __init__(mcls, name, bases, attrs):
def logger(self, msg):
print msg
mcls.log = logger
class Loggable(object):
__metaclass__ = MetaClassLogger
def method_one(self, data):
self.log(data)
def method_two(self):
self.log('Operation Succeeded.')
loggable = Loggable()
loggable.method_one('Test')
loggable.method_two()
When writing larger programs, break the code up into modules and packages.
import time import time as T from time import localtime from time import *
__init__.py.import dirname.subdirname.modulename.The VPython example program.
import urllib import re url = 'http://weather.noaa.gov/weather/current/KNYC.html' webpage = urllib.urlopen(url).read() match = re.search(r'(-?\d+(?:\.\d+)?) F',webpage,re.S) print 'In New York, it is now',match.group(1),'degrees.'
from mod_python import apache
def handler(req):
req.content_type = 'text/plain'
req.write('Hello, World')
return apache.OK
Make sure you validate all user input -- such as
req.filename below -- because it may contain data designed to
break your system!
from mod_python import apache
def handler(req):
req.content_type = 'text/plain'
req.write('You requested: '+req.filename)
return apache.OK
from mod_python import apache
from mod_python import Session
def handler(req):
req.content_type = 'text/plain'
session = Session.Session(req)
if session.is_new():
session['addr'] = req.connection.remote_ip
session.save()
req.write('Hello, World')
else:
req.write('Hello Again!')
return apache.OK
from mod_python import apache
from mod_python import util
def handler(req):
req.content_type = 'text/plain'
fields = util.FieldStorage(req)
name = fields.getfirst('name', '').strip()
if name:
if (not name.isalnum()) or (len(name) > 50):
raise Exception, 'Malformed Name'
req.content_type = 'text/plain'
req.write('Hello, '+name)
return apache.OK