Python primer - comments appreciated!
Thorsten Kampe
thorsten at thorstenkampe.de
Tue Sep 10 07:55:47 EDT 2002
* James J. Besemer
> Thorsten Kampe wrote:
>
>> "Python, anyone?"... Yep, this is my first one. Comments on
>> style/readability and technique/flow appreciated!
> [...]
>> # put this in one line?
>> true_class = []
>> false_class = []
> [...]
> #### 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
Well, uh, ah, eh, errm, good to know, thanks!
>> quotient_set.append([item])
>
> #### teeny tiny nit: I prefer "quotient_set.extend( item )" to the above.
That's not the same: I'm appending a new equivalence list to the
quotient set by making item a list and your version would append a
list as individual items.
>> # 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.
Well I RTFM'd and 'tutorialed' a lot, but I cannot find any concrete
use for this OOP thing. Could you elaborate?!
> #### if you are 'indexing' you may want to use a dictionary base type
> instead of a list.
I think I'm not "indexing" but "hashing" over the representants, so a
dictionary (associative array) should be the natural thing, thanks.
> #### if you only want to test membership, instead of the 'try' you can
> test:
>
> if representative in representative_class: ....
Well, even "if item in seq: seq.index(item)" is faster than "try:
seq.index(item)"
Thorsten
More information about the Python-list
mailing list