Python primer - comments appreciated!

James J. Besemer jb at cascade-sys.com
Tue Sep 10 03:02:58 CEST 2002



Thorsten Kampe wrote:

>"Python, anyone?"... Yep, this is my first one. Comments on
>style/readability and technique/flow appreciated!
>
Looks good overall, certainly you're off to a good start.

I added a few remarks/answers below...

--jb

>
>
>#v+
>def quotient_set(seq, func, partition='nonbool'):
>    """ partition <seq> into equivalence classes
>
>    With the 'bool' parameter 'quotient_set' always returns two lists: the
>    first  containing the 'true' items and the second containing the false
>    ones. """
>    if partition == 'bool':
>    # Yes, I know about 'filter', but this returns just the 'true
>    # elements'...
>    
>        # put this in one line?
>        true_class  = []
>        false_class = []
>

        #### if you are thinking "true_class = []; false_class - []" 
then I'd guess
        #### most here would recommend against bunching up as pointless
        #### and just a tad less readable than two separate statements.

        #### if you are thinking "true_class = false_class = []"
        #### then it would be a BIG NO-NO.
        #### the latter form initializes both variables to the SAME 
(empty) list
        #### and I'm pretty sure you want 2 separate lists.  

        #### 'Mutable' constant objects like [] and {} work differently from
        #### 'Immutable' ones like 1, 0.0 and "abc".  Not sure the 
precise Pythonetics
        #### terminology, but one way to think of it is that mutable 
types are accessed
        #### 'by reference' while immutableones are accessed as if they 
were 'by value'.
        #### 2 + 3 results in a new number.  list.append(...) changes 
the list in place and
        #### affects all 'references' to the list.  

>
>        for item in seq:
>            if func(item):
>                true_class.append(item)
>            else:
>                false_class.append(item)
>        return [true_class, false_class]
>        
>    else:
>        # put this in one line?
>        quotient_set =         []
>        representative_class = []
>
>	for item in seq:
>            representative = func(item)
>
>            # "Try: seq.index(item)" is IMO rather ugly in a loop. But
>            # it's even better than "if item in seq: seq.index(item)",
>            # which goes twice through seq
>            try:
>                quotient_set_index = representative_class.index(representative)
>            except ValueError:
>                quotient_set.append([item])
>

#### teeny tiny nit:  I prefer "quotient_set.extend( item )" to the above.

>
>                representative_class.append(representative)            
>            else:
>                # This somehow "inelegant", because all I want to do is:
>                # "seq[index] = seq[index].append(item)" Suggestions?
>

####  Define your set or sequence types to be a class or classes.  
####  You can then define [] and .append() operations
####  to be whatever you want (e.g., silently exclude duplicates, etc.)

####  Even the most trivial data abstractions usually benefit by being 
made an
####  explicit class/object.

####  if you are 'indexing' you may want to use a dictionary base type 
instead of a list.

####  if you only want to test membership, instead of the 'try' you can 
test:

                  if representative in representative_class:  ....  


>
>                equivalence_class = quotient_set[quotient_set_index]
>                equivalence_class.append(item)
>                quotient_set[quotient_set_index] = equivalence_class
>                
>        return quotient_set
>#v-
>
>To get a feeling for what 'quotient_set' does, try:
>
>>>>f = lambda x: x % 3
>>>>quotient_set([1, 2, 3, 4, 5, 6, 7, 8, 9], f)
>>>>
>[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>
>>>>quotient_set([1, 2, 3, 4, 5, 6, 7, 8, 9], f, 'bool')
>>>>
>[[1, 2, 4, 5, 7, 8], [3, 6, 9]]
>
>#v+
>def set(seq):
>    """ make <seq> a true set by removing duplicates """
>    # Shortest way to do a 'null function'? 'None' doesn't work...
>    f = lambda x: x  # identity function
>    
>    return [item[0] for item in quotient_set(seq, f)]    
>#v-
>

-- 
James J. Besemer		503-280-0838 voice
2727 NE Skidmore St.		503-280-0375 fax
Portland, Oregon 97211-6557	mailto:jb at cascade-sys.com
				http://cascade-sys.com	


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020909/60d4e36a/attachment.html>


More information about the Python-list mailing list