David Wurmfeld wrote:
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.
To actually answer your question, no, there is no standard for enums in python. There are custom hacks for it that you can search for.
Boo, a programming language that is virtually identical to python, does have standard enums:
enum Color: Red Green Blue
See http://boo.codehaus.org/
In fact, since not many seem to be aware of its existence, I encourage everyone here to check out boo as an alternative to python.
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 (<, >)
No, not with the == operator, unless you use: s1.lower() == s2.lower()
Visual Basic is the only language I am aware of that has case-insensitive strings.
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?
Because python is a strongly typed. If you want to perform a type specific operation like abs() or string.lower(), but the object's type may not be the right type, then you have to check its type first.
In boo, we have an "isa" operator for this purpose:
if x isa string: ....
or: for item in myList: given typeof(item): when string: print item.ToLower() when int: print Math.Abs(item) -- http://mail.python.org/mailman/listinfo/python-list