Re: How complex is complex?
In article mailman.2257.1237534775.11746.python-l...@python.org, Hendrik van Rooyen m...@microcorp.co.za wrote: Aahz a...@pyft.com wrote: 8 .. Because the name Python is derived from the comedy TV show Monty Python, stupid jokes are common in the Python community.) Sacrilege! A joke based on the Monty Python series is BY DEFINITION not stupid! Where did I say anything about jokes BASED ON Monty Python? -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-) --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-3-22 -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 18, 12:30 pm, Kottiyath n.kottiy...@gmail.com wrote: When we say readability counts over complexity, how do we define what level of complexity is ok? [snip= mommie can i go out an play?] How do we decide whether a level of complexity is Ok or not? Hmm? How did you know what shoes to put on this morning or what to eat for breakfast? I'll tell you, you made a choice that was best suited for you, thats how. Do you really need the opinion of others before making a decision this simple. Have the people of the world become so needy that they cannot even choose between pink panties with purple polka- dots or purple panties with pink polka-dots anymore without asking for input?. It seems seems the world has become a dimension of drooling mindless zombies wondering aimlessly through life who can even think for them selfs. God help us all!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
this is hardly a fair reply to a fair question. the question of time, space, or algorithmic complexity comes up all the time in the choice of which algorithm or data structure is best suited to attack a problem. donald knuth's anaylses of the computational complexity of algorithms has provided us with the tools to answer how complex an algorithm is and to compare its complexity to another algorithm. whoever wants to deal with issues of complexity might want to start there. knowing the complexity of an algorithm provides a starting point for answering whether the level of complexity the algorithm adds to a program makes it acceptable for use or not, or whether using it will be ok to run against small datasets but not against large; or even whether another attack on the problem to be solved should be considered. i dare say, the question what level of complexity is acceptable is a fair question, and the possible answers to choose from is the bread-and-butter of computer sciences. again, for those who want to get a handle on the issue, start with donald knuth's work or introductory texts that build on his work. -james Quoting r rt8...@gmail.com: On Mar 18, 12:30 pm, Kottiyath n.kottiy...@gmail.com wrote: When we say readability counts over complexity, how do we define what level of complexity is ok? [snip= mommie can i go out an play?] How do we decide whether a level of complexity is Ok or not? Hmm? How did you know what shoes to put on this morning or what to eat for breakfast? I'll tell you, you made a choice that was best suited for you, thats how. Do you really need the opinion of others before making a decision this simple. Have the people of the world become so needy that they cannot even choose between pink panties with purple polka- dots or purple panties with pink polka-dots anymore without asking for input?. It seems seems the world has become a dimension of drooling mindless zombies wondering aimlessly through life who can even think for them selfs. God help us all!!! -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
ja...@biosci.utexas.edu writes: donald knuth's anaylses of the computational complexity of algorithms I think the question was about how intricate the algorithm was (this affects its difficulty of implementation and understanding), not its computational complexity. -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
thanks, paul. if i understand correctly, questions about about how intricate [an] algorithm [is] (this affects its difficulty of implementation and understanding) are also fair and deserve fair answers. again, if i understand correctly, this issue gets its share of attention in computer sciences, and cs teachers discuss it in class, passing along their own appreciation of it to their students. - james Quoting Paul Rubin http://phr.cx@NOSPAM.invalid: ja...@biosci.utexas.edu writes: donald knuth's anaylses of the computational complexity of algorithms I think the question was about how intricate the algorithm was (this affects its difficulty of implementation and understanding), not its computational complexity. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
ja...@biosci.utexas.edu writes: again, if i understand correctly, this issue gets its share of attention in computer sciences, and cs teachers discuss it in class, passing along their own appreciation of it to their students. I think that question has nothing to do with CS (basically a theoretical subject about algorithms and so forth). It's more of a software practice issue. Of course, software practice is sometimes taught in CS departments, but that is basically for historical reasons. -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
thanks, paul. again, since i don't know the context of the original question, i may be speaking to something different than the original post; however, questions about about how intricate [an] algorithm [is] (this affects its difficulty of implementation and understanding) are indeed discussed in introductory cs courses. and in those courses, as well as later courses, programming assignments get point deductions when the student programmer fails to follow good software practice. insofar as penalties guide learning or encourage students to follow best practices, the teaching of software practices continues throughout the cs degree program. if the original poster wants to email me off-list both the question and its context, i'm willing to see if i have anything useful to contribute. -james Quoting Paul Rubin http://phr.cx@NOSPAM.invalid: ja...@biosci.utexas.edu writes: again, if i understand correctly, this issue gets its share of attention in computer sciences, and cs teachers discuss it in class, passing along their own appreciation of it to their students. I think that question has nothing to do with CS (basically a theoretical subject about algorithms and so forth). It's more of a software practice issue. Of course, software practice is sometimes taught in CS departments, but that is basically for historical reasons. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
Terry Reedy tjre...@udel.edu wrote: Vito De Tullio wrote: Tim Roberts wrote: bearophileh...@lycos.com wrote: In Python 3 those lines become shorter: for k, v in a.items(): {k: v+1 for k, v in a.items()} This is nonsensical. It creates and discards a complete new dict for each item in the original dict. The reuse of names 'k' and 'v' in the comprehension just confuse. You have to look back at the original post in which those lines appeared, and then look back further at the post which they were commenting on. Those two lines do not constitute consecutive lines of code, they are individual replacements for individual lines in two different previous examples, one of which updates the dict in place and the other of which creates a new dict. I think bearophile left out too much context :) -- R. David Murray http://www.bitdance.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
Aahz a...@pyft.com wrote: 8 .. Because the name Python is derived from the comedy TV show Monty Python, stupid jokes are common in the Python community.) Sacrilege! A joke based on the Monty Python series is BY DEFINITION not stupid! :-) - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
2009/3/20 Hendrik van Rooyen m...@microcorp.co.za: A joke based on the Monty Python series is BY DEFINITION not stupid! But may get /too/ silly. -- Tim Rowe -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
Tim Roberts wrote: bearophileh...@lycos.com wrote: In Python 3 those lines become shorter: for k, v in a.items(): {k: v+1 for k, v in a.items()} That's a syntax I have not seen in the 2-to-3 difference docs, so I'm not familiar with it. How does that cause a to be updated? I think he would write a = { 'a': 4, 'c': 6, 'b': 5 } a = { k:v+1 for k, v in a.items() } a {'a': 5, 'c': 7, 'b': 6} -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
Vito De Tullio wrote: Tim Roberts wrote: bearophileh...@lycos.com wrote: In Python 3 those lines become shorter: for k, v in a.items(): {k: v+1 for k, v in a.items()} This is nonsensical. It creates and discards a complete new dict for each item in the original dict. The reuse of names 'k' and 'v' in the comprehension just confuse. That's a syntax I have not seen in the 2-to-3 difference docs, so I'm not familiar with it. How does that cause a to be updated? It does not. I think he would write a = { 'a': 4, 'c': 6, 'b': 5 } a = { k:v+1 for k, v in a.items() } a {'a': 5, 'c': 7, 'b': 6} This *replaces* original dict a with a new dict rather than updating (its values) in place. This is less efficient. If there are other references to the original dict, the rebinding may or may not be correct. I believe the in-place update was already given as: for k,v in a.items(): a[k] = v+1 # or for k in a.keys(): a[k] += 1 Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
When we say readability counts over complexity, how do we define what level of complexity is ok? For example: Say I have dict a = {'a': 2, 'c': 4, 'b': 3} I want to increment the values by 1 for all keys in the dictionary. So, should we do: for key in a: ... a[key] = a[key] + 1 or is it Ok to have code like: dict(map(lambda key: (key, a[key] + 1), a)) Before doing anything else I'd suggest leaving your code as is, closing your editor immediately and not touching it at all before the One True Answer arrives from the PSF. Please mail your question (along with a self-addressed envelope) to: Python Software Foundation P.O. Box 848 Hampton, NH 03843 USA where python language lawyers will consider it in great detail, consulting GvR if necessary. Please do not try to figure this one out by yourself! The PSF lawyers are trained to do this, such things are better left to professionals, you don't want to shoot yourself in the foot. Once every nuanced detail has been carefully weighed in and a consensus has been reached among the Supreme Python Language Commission chamber of the PSF the appropriate answer will be mailed back to you. Now you should be able to open up your favorite editor and hack away knowing full well that nobody and nothing can stop you, ever! Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 11:29 am, Daniel Fetchinson fetchin...@googlemail.com wrote: When we say readability counts over complexity, how do we define what level of complexity is ok? For example: Say I have dict a = {'a': 2, 'c': 4, 'b': 3} I want to increment the values by 1 for all keys in the dictionary. So, should we do: for key in a: ... a[key] = a[key] + 1 or is it Ok to have code like: dict(map(lambda key: (key, a[key] + 1), a)) Before doing anything else I'd suggest leaving your code as is, closing your editor immediately and not touching it at all before the One True Answer arrives from the PSF. Please mail your question (along with a self-addressed envelope) to: Python Software Foundation P.O. Box 848 Hampton, NH 03843 USA where python language lawyers will consider it in great detail, consulting GvR if necessary. Please do not try to figure this one out by yourself! The PSF lawyers are trained to do this, such things are better left to professionals, you don't want to shoot yourself in the foot. Once every nuanced detail has been carefully weighed in and a consensus has been reached among the Supreme Python Language Commission chamber of the PSF the appropriate answer will be mailed back to you. Now you should be able to open up your favorite editor and hack away knowing full well that nobody and nothing can stop you, ever! Cheers, Daniel -- Psss, psss, put it down! -http://www.cafepress.com/putitdown I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Thu, Mar 19, 2009 at 9:39 AM, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. -- http://mail.python.org/mailman/listinfo/python-list I didn't think it was a foolish question, just one with a very subjective (and perhaps unknowable) answer. I have a feeling that Daniel wasn't intending to come off as snarky, but who knows. What someone considers 'readable' or 'complex' is going to vary from individual to individual. If you look at it (or if a peer on roughly the same skill level as you looks at it) and has to squint really hard to figure out what it's doing, it's probably not readable enough - which may or may not be linked to the complexity of the code itself (Depending on how what you mean by complexity - number of instructions? number of characters? special charachters? number of tokens? levels of indirection? etc). -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
[posted and e-mailed] In article 033514d1-e0e9-4a1c-bca0-846781f0d...@w35g2000prg.googlegroups.com, Kottiyath n.kottiy...@gmail.com wrote: On Mar 19, 11:29=A0am, Daniel Fetchinson fetchin...@googlemail.com wrote: Once every nuanced detail has been carefully weighed in and a consensus has been reached among the Supreme Python Language Commission chamber of the PSF the appropriate answer will be mailed back to you. Now you should be able to open up your favorite editor and hack away knowing full well that nobody and nothing can stop you, ever! I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. No! Your question was *not* foolish, it was in fact a very good question; Daniel was just making a joke in somewhat dubious taste. (Not in such poor taste that Daniel should apologize, but I'm guessing that English is not your primary language, so it can be difficult to identify jokes here. Because the name Python is derived from the comedy TV show Monty Python, stupid jokes are common in the Python community.) -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ Programming language design is not a rational science. Most reasoning about it is at best rationalization of gut feelings, and at worst plain wrong. --GvR, python-ideas, 2009-3-1 -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 4:39 am, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 8:42 pm, Paul McGuire pt...@austin.rr.com wrote: On Mar 19, 4:39 am, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. -- Paul Daniel, Sorry for misunderstanding your post. I hope I was not being passive-aggresive - (also because I found that the second mechanism I provided was quite horrible :-), so I was indeed being foolish there. ) Paul/Aahz, I did understand 2 things (1) When using map always consider that the function will be called everytime, so the hit on the performance is more. (2) The second mechanism and the first mechanism provides different solutions (new dict/same dict) both of which I did not think about at all. Also, thank you everyone for all the help. I have been following this thread for the last 4 months (when I started with python) and I have learned a lot. The amount of help provided here is amazing. p.s. - English is indeed not my first language :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 9:33 pm, Kottiyath n.kottiy...@gmail.com wrote: On Mar 19, 8:42 pm, Paul McGuire pt...@austin.rr.com wrote: On Mar 19, 4:39 am, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. -- Paul Daniel, Sorry for misunderstanding your post. I hope I was not being passive-aggresive - (also because I found that the second mechanism I provided was quite horrible :-), so I was indeed being foolish there. ) Paul/Aahz, I did understand 2 things (1) When using map always consider that the function will be called everytime, so the hit on the performance is more. (2) The second mechanism and the first mechanism provides different solutions (new dict/same dict) both of which I did not think about at all. Also, thank you everyone for all the help. I have been following this thread for the last 4 months (when I started with python) and I have learned a lot. The amount of help provided here is amazing. p.s. - English is indeed not my first language :-) Oops, Forgot to mention the biggest learning. Readability is better than brevity - Thanks to Rhodri. This was a question which was bugging me all the time. When I look at code, I am always envious when I see the same code written in much smaller number of lines. Now, I will force myself to ask the questions Rhodri proposed (esp: does it look uglier part) before deciding whether or not to go ahead with brevity. -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 9:41 am, Kottiyath n.kottiy...@gmail.com wrote: On Mar 19, 9:33 pm, Kottiyath n.kottiy...@gmail.com wrote: On Mar 19, 8:42 pm, Paul McGuire pt...@austin.rr.com wrote: On Mar 19, 4:39 am, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. -- Paul Daniel, Sorry for misunderstanding your post. I hope I was not being passive-aggresive - (also because I found that the second mechanism I provided was quite horrible :-), so I was indeed being foolish there. ) Paul/Aahz, I did understand 2 things (1) When using map always consider that the function will be called everytime, so the hit on the performance is more. (2) The second mechanism and the first mechanism provides different solutions (new dict/same dict) both of which I did not think about at all. Also, thank you everyone for all the help. I have been following this thread for the last 4 months (when I started with python) and I have learned a lot. The amount of help provided here is amazing. p.s. - English is indeed not my first language :-) Oops, Forgot to mention the biggest learning. Readability is better than brevity - I rewrote your sentence to be more optimized. Readability brevity ;-) Thanks to Rhodri. This was a question which was bugging me all the time. When I look at code, I am always envious when I see the same code written in much smaller number of lines. Now, I will force myself to ask the questions Rhodri proposed (esp: does it look uglier part) before deciding whether or not to go ahead with brevity. -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 1:25 pm, Paul Hildebrandt paul_hildebra...@yahoo.com wrote: On Mar 19, 9:41 am, Kottiyath n.kottiy...@gmail.com wrote: On Mar 19, 9:33 pm, Kottiyath n.kottiy...@gmail.com wrote: On Mar 19, 8:42 pm, Paul McGuire pt...@austin.rr.com wrote: On Mar 19, 4:39 am, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. -- Paul Daniel, Sorry for misunderstanding your post. I hope I was not being passive-aggresive - (also because I found that the second mechanism I provided was quite horrible :-), so I was indeed being foolish there. ) Paul/Aahz, I did understand 2 things (1) When using map always consider that the function will be called everytime, so the hit on the performance is more. (2) The second mechanism and the first mechanism provides different solutions (new dict/same dict) both of which I did not think about at all. Also, thank you everyone for all the help. I have been following this thread for the last 4 months (when I started with python) and I have learned a lot. The amount of help provided here is amazing. p.s. - English is indeed not my first language :-) Oops, Forgot to mention the biggest learning. Readability is better than brevity - I rewrote your sentence to be more optimized. Readability brevity ;-) Thanks to Rhodri. This was a question which was bugging me all the time. When I look at code, I am always envious when I see the same code written in much smaller number of lines. Now, I will force myself to ask the questions Rhodri proposed (esp: does it look uglier part) before deciding whether or not to go ahead with brevity. sometimes: brevity==Readability but as in many things the trick is in finding the right tradeoff. I am willing to accept some trivial amount of additional complexity if it means I have to read less lines of code, but I have my limits too. I aim for a comprehension speed of 2-10 lines per minute for somebody proficient in the language. -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. Daniel, Sorry for misunderstanding your post. I hope I was not being passive-aggresive - (also because I found that the second mechanism I provided was quite horrible :-), so I was indeed being foolish there. ) My point was exactly what somebody already mentioned: with such subjective matters there is no way of deciding one way or another in a rational way. What I found is that once you completely discard these issues and don't waste brain cycles on them at all, but rather you just go ahead and code, you will be actually sorting these things out by yourself or put it in another way, these things will be sorted out by themselves. After all, GvR said things to the effect that the whole concept of language design is not rational or objective or scientific, but rather a big bag of gut feelings and I completely agree. Rationalizing about these things is pretty dubious to me. Have fun with python! Daniel Paul/Aahz, I did understand 2 things (1) When using map always consider that the function will be called everytime, so the hit on the performance is more. (2) The second mechanism and the first mechanism provides different solutions (new dict/same dict) both of which I did not think about at all. Also, thank you everyone for all the help. I have been following this thread for the last 4 months (when I started with python) and I have learned a lot. The amount of help provided here is amazing. p.s. - English is indeed not my first language :-) -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
How complex is complex?
When we say readability counts over complexity, how do we define what level of complexity is ok? For example: Say I have dict a = {'a': 2, 'c': 4, 'b': 3} I want to increment the values by 1 for all keys in the dictionary. So, should we do: for key in a: ... a[key] = a[key] + 1 or is it Ok to have code like: dict(map(lambda key: (key, a[key] + 1), a)) How do we decide whether a level of complexity is Ok or not? -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
Kottiyath: How do we decide whether a level of complexity is Ok or not? I don't understand your question, but here are better ways to do what you do: a = {'a': 2, 'c': 4, 'b': 3} for k, v in a.iteritems(): ... a[k] = v + 1 ... a {'a': 3, 'c': 5, 'b': 4} b = dict((k, v+1) for k, v in a.iteritems()) b {'a': 4, 'c': 6, 'b': 5} The first modifies the dict in-place, and the second created a new dict. In Python 3 those lines become shorter: for k, v in a.items(): {k: v+1 for k, v in a.items()} Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 18, 1:30 pm, Kottiyath n.kottiy...@gmail.com wrote: When we say readability counts over complexity, how do we define what level of complexity is ok? For example: Say I have dict a = {'a': 2, 'c': 4, 'b': 3} I want to increment the values by 1 for all keys in the dictionary. So, should we do: for key in a: ... a[key] = a[key] + 1 or is it Ok to have code like: dict(map(lambda key: (key, a[key] + 1), a)) How do we decide whether a level of complexity is Ok or not? This isn't just a question of readability; the two expressions are entirely different. The second expression creates a whole new dictionary, which might not have been obvious to you given the overall complexity of the expression. The first expression is simple, clear, and other than maybe changing a[key] = a[key] + 1 to a[key] += 1 is pretty much hard to improve on. If the number of lines matters to you (it shouldn't, be opinions vary), then you could always write: for k in a: a[k] += 1 Which is shorter and far easier to read than the dict/map/lambda expression. And probably what you really intended! -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 18, 1:30 pm, Kottiyath n.kottiy...@gmail.com wrote: When we say readability counts over complexity, how do we define what level of complexity is ok? For example: Say I have dict a = {'a': 2, 'c': 4, 'b': 3} I want to increment the values by 1 for all keys in the dictionary. So, should we do: for key in a: ... a[key] = a[key] + 1 or is it Ok to have code like: dict(map(lambda key: (key, a[key] + 1), a)) How do we decide whether a level of complexity is Ok or not? The second alternative is: - unreadable (took me 10 seconds to parse vs 1 for the former). - slower (makes a function call on every round). - broken (creates a new dict instead of modifying the original in place). Really, there's not much of a dilemma here. George -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
You realize of course that these two alternatives are not equivalent. The first does what your problem statement describes, for each key in a given dict, increments the corresponding value. The second creates an entirely new dict with the modified values. Even if you were to write the second one as a = dict(map(lambda key: (key, a[key] + 1), a)) This would not necessarily accomplish the same effect as the for loop. If a is an argument to a function, then the for-loop actually updates the given dict in place, so that the effects of the increment-by-one for loop will be seen in the caller after this function ends. However, constructing a new dict and assigning to 'a' only affects the value of a in the local function - the caller's dict will be unaffected. For updating in place, as in your first example, I am hard-pressed to come up with a simpler form (ah, thank you bearophile for looping over iteritems instead of the keys). But if constructing a new dict is an acceptable approach, then the dict/map/lambda approach you have posted is functional overkill. To do the iteration over 'a' that map does with the lambda, you may as well do with a list comprehension, in far more readable form: a = dict((k,v+1) for k,v in a.iteritems()) If you are using Py2.6 or 3.0, you can use the new dict comprehension form: a = {k:v+1 for k,v in a.iteritems()} Would you *really* want to take the position that the map/lambda form is easier to follow than this? -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Wed, 18 Mar 2009 17:30:45 -, Kottiyath n.kottiy...@gmail.com wrote: When we say readability counts over complexity, how do we define what level of complexity is ok? We don't. There is no One True Way that defines for us what complexity is, never mind how much of it is too much. It's a judgement call that is very personal, and as with all these things involves trade-offs. Some potentially helpful rules of thumb: * Am I going to understand this code when I come back to it in half an hour? If not, I'm probably better off with a more readable version or (if I really must) a page and a half of comments explaning the complex version. * Am I going to understand this code when I come back to it in six months? Similar reasoning applies, though I'm more likely to comment than replace in this case. * Is someone else going to understand this code in six months time when I won't be around to help them. A rather higher bar than the previous one, which pushes back towards readability again. * Does it look incredibly ugly? If so, it's probably a bad idea no matter what. * (with respect to your example) Does it do the same thing as the more readable version, and do it at least as efficiently? If not, there's no point in being clever, do it the readable way. -- Rhodri James *-* Wildebeeste Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
bearophileh...@lycos.com wrote: In Python 3 those lines become shorter: for k, v in a.items(): {k: v+1 for k, v in a.items()} That's a syntax I have not seen in the 2-to-3 difference docs, so I'm not familiar with it. How does that cause a to be updated? -- Tim Roberts, t...@probo.com Providenza Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list