Base classes or global functions

Stephen Boulet stephen at theboulets.net
Mon Jul 29 00:50:38 EDT 2002


Thanks for the additional responses.

The types of functions I was talking about are just some utility functions 
for working with strings and lists. Here are a couple:

def findUntil(str, f):
        """
        Return the firstline in a file containing 'str'
        """
        while(1):
                line = f.readline()
                if line == '':
                        print "Ran all the way through the file",
                        print "without finding '" + str + "'"
                        return -1
                if line.find(str) != -1:
                        break
        return line

def rremove(aList, item):
        """
        Remove from a list all occurences of 'item'
        """
        while(1):
                try:
                        aList.remove(item)
                except ValueError:
                        break

I did implement Alex Martelli's suggestion (put them in utilityFuncs.py, and 
do an "import utilityFuncs". This has the advantage of making my namespace 
cleaner.

Is that way of doing it better or worse than:

class BaseClass:
        def __init__(self):
                pass
        
        def findUntil(self,str, f):
                """
                Return the firstline in a file containing 'str'
                """
                while(1):
                        line = f.readline()
        
                        if line == '':
                                print "Ran all the way through the file",
                                print "without finding '" + str + "'"
                                return -1
                        if line.find(str) != -1:
                                break
                return line

        def rremove(self,aList, item):
                """
                Remove from a list all occurences of 'item'
                """
                while(1):
                        try:
                                aList.remove(item)
                        except ValueError:
                                break

class Class1(BaseClass):
        ... etc...

Both ways work. Oh no, does that mean there is more than one way to do it? 
;)

-- Stephen

Robb Shecter wrote:

> Stephen Boulet wrote:
>> I have two classes, Class1 and Class2. Class1 contains a list of Class2
>> objects, but no inheritance is involved
>> 
>> Both classes use some functions I wrote. What's the best way of making
>> the functions available to the classes?
>> 
> 
> Good questions.  To answer it, we'd have to know what the functions do -
> what information they manipulate.  Depending on your answer, any of the
> following scenarios are possible:
> 
> 1. The functions become methods of Class1.
> 2. The functions become methods of Class2.
> 3. The functions become methods of a new, as yet unwritten class.
> 3a. This new class would be used by Class1 and Class2
> 3b. This new class would be subclassed by either Class1 or Class2.
> 
> Robb




More information about the Python-list mailing list