[Tutor] Sets and Python; new shiny tool syndrome?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Dec 28 04:10:21 CET 2005


> I just tried out sets for the first time, and I'm finding multiple uses
> for them, particularly for replacing and simplifying what would normally
> be one or two list comprehensions i.e.
>
> 	def parseKws(self, kw_data):
> 		ignoreSet = set(['import', 'from', 'as', ' ', ''])
> 		kws = set([])
> 		for line in kw_data:
> 			line = line.rstrip("\n")
> 			if "," in line: 	line = line.replace(",", " ")
> 			if ";" in line: 	line = line.replace(";", " ")

Hi Liam,

Quick comment: the two if statements there can be simplified by just doing
the replacement straight-out:

#############################
line = line.replace(",", " ")
line = line.replace(";", " ")
#############################

The reason is that if those characters aren't present, no harm is done.


But looking at the code, I'm curious: it looks like you're trying to
tokenize the keywords out of Python source?  If so, you may want to look
at the 'tokenize' module:

    http://www.python.org/doc/lib/module-tokenize.html

as it'll handle some of the especially tricky cases like handling string
literals.


> However, I'm reminded of the joke about you can tell what chapter
> someone reading the Patterns book by the Gang of Four is up to by what
> new pattern they're trying to use the next day, no matter the problem.
>
> Are there any drawbacks to sets that I need to watch out for?

Use them when they're appropriate, and don't use them when they're not.
*grin*

It really is problem sensitive: if order matters, then sets probably
aren't appropriate.  But sets are such a pervasive concept that few
problems don't provide opportunities to take advantage of them.

Happy holidays to you!



More information about the Tutor mailing list