[Discuss] Functional programming @ UVic (was 'C' string tokenizer for those who hate strtok)

Brian Quinlan brian at sweetapp.com
Mon Jul 3 13:14:52 PDT 2006


Adam Parkin wrote:
> Really?  So functions are first class values and can be passed 
> around/returned from other functions?

I should also point out that functions are just objects with attributes 
and methods, like everything else e.g.

 >>> from urllib2 import urlopen
 >>> dir(urlopen)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', 
'__get__', '__getattribute__', '__hash__', '__init__', '__module__', 
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 
'func_dict', 'func_doc', 'func_globals', 'func_name']
 >>> urlopen.func_name
'urlopen'
 >>> urlopen.func_code.co_filename
'/usr/lib/python2.4/urllib2.py'
 >>> urlopen.func_code.co_firstlineno
125
 >>> urlopen.func_code.co_varnames
('url', 'data')
 >>> urlopen('http://www.apple.com').read(60)
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E'

Cheers,
Brian


More information about the Discuss mailing list