newbie question

David Wurmfeld dwurmfeld at cfl.rr.com
Sun Dec 19 07:04:34 EST 2004


I am new to python; any insight on the following would be appreciated, even 
if it is the admonition to RTFM (as long as you can direct me to a relevant 
FM)

Is there a standard approach to enumerated types? I could create a 
dictionary with a linear set of keys, but isn't this overkill? There is 
afterall a "True" and "False" enumeration for Boolean.

Is there a way to ignore case in string comparisons? I want 'Oranges' to 
equal 'oranges' when using the evaluation operator (==). I don't care about 
string inequalities (<, >)

If I am compelled to use dictionaries for enumerated types, is there a way 
to generate unique keys automatically: something like 
"myDict.appendWithAutoKey("Persimmons")"?

Is there a way to overload operators to accommodate an enumeration? For 
example,

newFruit = enumerationFruit('Cumquat')        #use it like you would use 
list()
new = newFruit + 14        # this should throw an exception because of 
different types

Finally, (for now at least) consider the following list.

myList = [apple, 13, plum, cherry, 'Spam', tomato, 3.35]

Exactly how does the "for x in myList" work?
If the list is a heterogeneous list of disparate types, the == operator 
works fine, independent of type.
For example, (if x == 'spam') evaluates as false if the item in the list is 
an integer. But if I try to do this: (if x.__abs()__) throws an exception if 
x "pulls" a non integer from the list. Wouldn't you think that an iterative 
would have the "sense" to understand that in this limited scope if a method 
didn't apply to the iterator just "fail" (i.e. evaluate to False) the 
evaluation and move along? Do I have to manually interrogate each iteration 
for the proper type before I test?
Think about it; the interpreter has to evaluate disparate types for 
equality. How exactly does the it "know" that for this iteration, x is an 
integer, and the evaluation (if x == 'spam') is False, and doesn't throw an 
exception for a type mismatch?

Thanks in advance for your insight,
David, Melbourne, Florida. 





More information about the Python-list mailing list