k-sok (3) [Avatar] Offline
#1
Hello!

I'm new to python and and prepare for my next project by reading your book. So far I've been coding in C/C++ and didn't have much to to with languages that magically make one's life easy, like python smilie In general, I considered that performance is not a big issue in such language (at least compared to C/C++ where performance is central and often the only reason for using the language). All the more it surprised me to read statements like "don't worry, dictionaries are implemented very efficiently in python" etc. which make one think that performance on the language constructs level _is_ something that a python programmer is (and should be) concerned with.

So given that, it suprised me even more to see an example like in 14.2.8 where exceptions are used for validation (and more or less control-flow). Coming from the C/C++ world one regards the occurrence of exceptions as considerable runtime overhead that should be accepted only in _exceptional_ (as its name implies) cases. It's funny but I would consider your example as an excellent case for not using exceptions because having empty cells is the normal case an now way exceptional smilie

So my question is: should I, as a python programmer, just forget all that burdening C++-exception-overhead bullshit? smilie

Thanks in advance
Konstantin
naomi.ceder (134) [Avatar] Offline
#2
Re: Pyhton philosophy = using exceptions for everything?
Ah, you ask some very good questions, full of potential traps... smilie

There are a couple of things I think it helps to keep in mind when moving from a language like C to a higher level language like Python....

First of all, 99% of the time performance is NOT something to be concerned with... and if you've profiled the code and found it IS a concern, THEN one could either optimize the code or even replace it with C (for just that portion). My point with that is that worrying about the low level details and then trying to beat the system, as it were, is NOT worth the effort. Basically, I've seen C programmers try to write C in Python and that code is more complex and LESS efficient than code that uses the features of the language well. On some level I would agree that a programmers should ALWAYS be concerned with performance, but only with relatively stable and debugged code - worrying about performance too much and too early is usually counter productive.

Indeed, the Python habit is to use exceptions far more heavily than a language like C/C++ and they are quite often used for flow control. This does not have the same performance hit in Python, and is in fact a keystone of Python's "easier to ask for forgiveness than permission" philosophy. So if you want to really write Pythonic code, I'd suggest you try to assimilate the patterns of using exceptions... I have to admit as an oldtimer with experience in other languages, I don't always use exceptions as fully as I should, myself.

Finally, while I wouldn't call it bullshit (not when you're coding in C/C++) I would say that using the language as it's intended/designed is the key to getting the most out of it. Writing C in Python will work and will probably even have advantages over writing in C in many cases, but writing good PYTHON code in Python will work even better!

I hope this answers your question (and avoids the minefields!).

Thanks for raising this question!

Vern
k-sok (3) [Avatar] Offline
#3
Re: Pyhton philosophy = using exceptions for everything?
Thank you for your response!

I guess I got my answer. Just wanted to say that I'm of course aware and completely agree with you that premature optimization is evil in any language. The only reason I raised the question is that I was a little bit confused by the fact that the reader is told something about the performance of e.g. dictionaries but nothing about impact of exceptions ( I know that somebody who has never heard of exceptions will never raise that question, it's my C++ burden).

Apart from that, I wouldn't dream of trying to transfer the philosophy of one language to another and to be honest, I like the way in which exceptions are intended to be used in pyhton. Indeed, it allows for some elegant solutions, as in your example.

Regards
Konstantin
naomi.ceder (134) [Avatar] Offline
#4
Re: Pyhton philosophy = using exceptions for everything?
I guess I should add that particularly in the old days, C programmers moving to Python were suspicious that the native structures (lists and dictionaries) were somehow too slow to be useful, so that comment is aimed at them. In fact the language itself relies extensively on dictionaries so they are one of the most optimized things in Python. But perhaps you are right, maybe we are now at a point where we don't need to argue this point any longer.
lnwpen (1) [Avatar] Offline
#5
Re: Pyhton philosophy = using exceptions for everything?