newbie question

Miki Tebeka miki.tebeka at zoran.com
Sun Dec 19 07:33:55 EST 2004


Hello David,

> 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.
Google for Python + enum.
(A good one is: http://www.norvig.com/python-iaq.html)
 
> 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 (<, >)
s1.lower() == s2.lower()
 
> If I am compelled to use dictionaries for enumerated types, is there a way 
> to generate unique keys automatically: something like 
> "myDict.appendWithAutoKey("Persimmons")"?
http://docs.python.org/lib/typesmapping.html and "setdefault"
 
> 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
http://www.python.org/doc/2.3.4/ref/numeric-types.html

 
> 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?
It binds 'x' in the body of the loop to each item in sequence.

> 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?
How would it konw that a method don't apply to an object?

> Do I have to manually interrogate each iteration 
> for the proper type before I test?
Yes. Or you can filter the sequence:
for x in [i for i in list if type(i) == type(1)]

> 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?
Python is strongly type in the sense that each object has a type.
I don't remember exactly how does Python resolve operators and methods, but
if it's can't find one for == it'll return False (IMO).

Bye.
--
------------------------------------------------------------------------
Miki Tebeka <miki.tebeka at zoran.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys



More information about the Python-list mailing list