please help shrink this each_with_index() implementation

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Jan 5 23:52:12 EST 2010


On Tue, 05 Jan 2010 15:30:09 -0800, Phlip wrote:

>> > Does it say how to convert a string containing either an integer
>> > representation, or something alphabetic, into an integer, or a zero,
>> > in like 1 method call? (No except: ?)
>>
>> If you mean something like this:
>>
>> >>> int('153')
>>
>> 153
> 
> The point: int('') or int('something') both throw an error.

Well, that's a bug. Obviously they should return 42.

Or is that 23? I forget.

But seriously... of course they do. What did you expect them to do?

Any Python function will raise an exception if you pass invalid data to 
it. So the solutions are, don't pass invalid data, or wrap the function 
in a try block. If you don't want to use try, then make sure that your 
data is good.



> In general,
> this is hand-holding, but in specific I don't think the "rich and
> structured" documentation will cover how to beat a 0 out of it in less
> than 3 lines. So I will persist in my idiotic questions here!
> 
>> Then perhaps you should work through the tutorial to learn the basics.
> 
> They will tell me how to use except: (which is a good example why a
> program should not use exceptions for its normal control flow if at all
> possible).

Huh? So because YOU don't know how to use except, programs shouldn't use 
exceptions for flow control? I reject that, and I will continue using 
exceptions for flow control when I think it is appropriate.


> Please, please, please save your newbie admonitions for those who
> qualify!

Oh please, get off your high horse. You're asking newbie questions, no 
matter how many years of using Python you may or may not have, your 
experience is obviously low. Stick around and you might learn something, 
but if you bite every time somebody tries to teach you, you'll soon run 
out of people willing to help you.



-- 
Steven



More information about the Python-list mailing list