[Tutor] do_it()

Edward Comber comber at cix.co.uk
Thu Dec 11 13:02:27 EST 2003


Fair enough, but you could have kept the list of words external to the
functions and used by both.

Also you can return two lists like...

def stop_n_go_words(terms):
	blah
	return stop_list, go_list

a_stop_list, a_go_list = stop_n_go_words(terms)

-----Original Message-----
From: tpc at csua.berkeley.edu [mailto:tpc at csua.berkeley.edu]
Sent: 11 December 2003 17:24
To: Eddie Comber
Cc: tutor at python.org
Subject: Re: [Tutor] do_it()



hi Eddie,

I was mulling this over recently, and while it is true the overhead is
much higher with OOP, the payoff is easier to read and more functional
code.  Let me give you an example.  I recently wrote a search engine that
takes as user input a query and returns results from an audio and video
archive.  One of the things I needed to do was filter common, or stop,
words (e.g., who, is, what, the, of) from go words that would be used in
other functions to look up and return results in the index.  I also
wanted to print out the words filtered to the user, so I had to either
duplicate the body of code for a function to return both, or I could
write a class called Filter and have two methods in it,
getUserGoWords(self) and getUserStopWords(self) using the same logic.  The
non-OOP way of doing this would be:

<paste>
def getUserGoWords(terms):
	stop_words_list = ['a', 'about', 'all', 'an', 'and', 'any', 'are',
'as', 'at', 'be', 'because', 'can', 'did', 'do', 'does', 'for', 'from',
'he', 'how', 'i', 'if', 'in', 'is', 'it', 'may', 'no', 'not', 'of', 'on',
'or', 'that', 'the', 'then', 'these', 'this', 'those', 'to', 'was', 'we',
'what', 'when', 'where', 'which', 'who', 'why', 'with']
	terms_list = terms.split()
	user_go_words = []
	for word in terms_list:
		if word.lower() not in stop_words_list:
			filtered_terms_list.append(word)
	return user_go_words

def getUserStopWords(terms):
	stop_words_list = ['a', 'about', 'all', 'an', 'and', 'any', 'are',
'as', 'at', 'be', 'because', 'can', 'did', 'do', 'does', 'for', 'from',
'he', 'how', 'i', 'if', 'in', 'is', 'it', 'may', 'no', 'not', 'of', 'on',
'or', 'that', 'the', 'then', 'these', 'this', 'those', 'to', 'was', 'we',
'what', 'when', 'where', 'which', 'who', 'why', 'with']
	terms_list = terms.split()
	user_stop_words = []
	for word in terms_list:
		if word.lower() in stop_words_list:
			user_stop_words.append(word)
	return user_stop_words
 </paste>

and the OOP way of doing this would be:

class Filter:
	def __init__(self, terms):
		# replace ' with '' for MySQL INSERT
		terms = terms.replace("'", "''")
		# this is currently how I handle context searches in Audio Video Archive
		terms = terms.replace('"', "")
		stop_words_list = ['&', 'a', 'about', 'all', 'an', 'and',
'any', 'are', 'as', 'at', 'be', 'because', 'can', 'do', 'does', 'for',
'from', 'he', 'how', 'i', 'if', 'in', 'is', 'it', 'may', 'no', 'not',
'of', 'on', 'or', 'that', 'the', 'then', 'these', 'this', 'those', 'to',
'was', 'we', 'what', 'when', 'where', 'which', 'who', 'why', 'with']
	        terms_list = terms.split()
		self.user_go_words = []
		self.user_stop_words = []
        	for word in terms_list:
        		if word.lower() not in stop_words_list:
        			self.user_go_words.append(word)
        		else:
                                self.user_stop_words.append(word)

	def getUserGoWords(self):
	        return self.user_go_words

	def getUserStopWords(self):
	        return self.user_stop_words

Maybe there is another way of doing this so that one function can return
two values, but as far as I know there is no doubt in my mind which makes
more sense with respect to future readers of my code and the holy mantra
of programming, Keep It Short and Simple.

I hope that helps you.

On Thu, 11 Dec 2003, Eddie Comber wrote:

> I see that often when people write OO code they have a main() something
> like:
>
> obj = myObject(param, param, param)
> obj.do_it()  #or obj.action() etc
>
> Are there any commanding advantages to making this sort of functionality
> into an object?
>
> The above example is easier written and used as:
>
> myfunc(param, param, param)
>
> I ask because I have just written a MAKE utility, and having started off
as
> an object I really don't know what the advantages are above a simple
> function.
>
> Best,
> Eddie.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list