[Edu-sig] Introspection + sets

Kirby Urner urnerk@qwest.net
Sat, 01 Mar 2003 11:39:26 -0800


Here's a way to abet exploration of Python primitives using introspection
with sets, new in 2.3:

   >>> import sets
   >>> j = sets.Set(dir([]))  # j is set of list methods
   >>> k = sets.Set(dir({}))  # k is set of dictionary methods

   >>> j.intersection(k)      # what lists and dictionaries have in common
   Set(['__ne__', '__getitem__', '__getattribute__', '__str__', '__reduce__',
   'pop', '__setitem__', '__gt__', '__lt__', '__eq__', '__init__',
   '__delitem__', '__setattr__', '__reduce_ex__', '__new__', '__contains__',
   '__iter__', '__class__', '__delattr__', '__le__', '__len__', '__hash__',
   '__doc__', '__ge__', '__repr__'])

   >>> j.difference(k)  # what lists have that dictionaries do not
   Set(['sort', 'index', '__getslice__', '__delslice__', '__imul__', 'extend',
   'insert', '__setslice__', 'count', 'remove', '__mul__', '__iadd__',
   '__add__', '__rmul__', 'append', 'reverse'])

   >>> k.difference(j)  # what dictionaries have that lists do not
   Set(['fromkeys', 'setdefault', 'get', 'keys', 'items', 'clear', 'popitem',
   'update', '__cmp__', 'has_key', 'values', 'itervalues', 'iteritems',
   'copy', 'iterkeys'])

Kinda fun.  Suggestive.

Kirby