Comments on: How not to write Python code http://eikke.com/how-not-to-write-python-code/ 'cause this is what I do Tue, 04 Dec 2012 00:03:23 +0000 hourly 1 http://wordpress.org/?v=3.4.1 By: Trevor http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-73812 Trevor Thu, 19 Apr 2012 14:42:53 +0000 http://eikke.com/how-not-to-write-python-code/#comment-73812 Regarding the switch statement discussion going on, Ale is correct, his points are all logically sound. The question comes down to this: is the concept of the switch statement a good one? The reason I say this is because you can apply his points to any programming construct, so if you believe the concept of switch is good (regardless of how poorly other languages implement it) then it should be added. Anyone who says otherwise is bias based on their emotions for some strange reason. Regarding the switch statement discussion going on, Ale is correct, his points are all logically sound. The question comes down to this: is the concept of the switch statement a good one?

The reason I say this is because you can apply his points to any programming construct, so if you believe the concept of switch is good (regardless of how poorly other languages implement it) then it should be added.

Anyone who says otherwise is bias based on their emotions for some strange reason.

]]>
By: Tom http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-33220 Tom Thu, 08 Apr 2010 20:18:40 +0000 http://eikke.com/how-not-to-write-python-code/#comment-33220 Another nice way to do a switch case is like this: def handle_one(): pass def handle_two(): pass someVariable = 'case1' switchcase = { 'case1':handle_one, 'case2':handle_two }[someVariable]() Another nice way to do a switch case is like this:

def handle_one():
pass
def handle_two():
pass

someVariable = ‘case1′
switchcase = {
‘case1′:handle_one,
‘case2′:handle_two
}[someVariable]()

]]>
By: dikar http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-2003 dikar Wed, 26 Mar 2008 05:47:18 +0000 http://eikke.com/how-not-to-write-python-code/#comment-2003 thanks thanks

]]>
By: Joe Smith http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-621 Joe Smith Tue, 12 Feb 2008 17:54:49 +0000 http://eikke.com/how-not-to-write-python-code/#comment-621 Sweet post! Thanks for the advice :) Sweet post! Thanks for the advice :)

]]>
By: Markus Jais http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-577 Markus Jais Mon, 11 Feb 2008 06:59:59 +0000 http://eikke.com/how-not-to-write-python-code/#comment-577 Great list. I especially agree with you about not reinventing the wheel. I've seen it dozens of times that programmers write libraries, modules and other stuff that was already availabe in the standard library. I've seen this in Python as well as Ruby, C++, Java and other languages. In most cases the solutions that already comes with the language (or is available on the web) is a very good and reliable solution and will always be better than the one you can create for yourself. Great list. I especially agree with you about not reinventing the wheel. I’ve seen it dozens of times that programmers write libraries, modules and other stuff that was already availabe in the standard library. I’ve seen this in Python as well as Ruby, C++, Java and other languages.
In most cases the solutions that already comes with the language (or is available on the web) is a very good and reliable solution and will always be better than the one you can create for yourself.

]]>
By: Paddy3118 http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-553 Paddy3118 Sun, 10 Feb 2008 16:55:26 +0000 http://eikke.com/how-not-to-write-python-code/#comment-553 Hi ale, I do not think the lack of an explicit switch statement is a major failing of Python. Whether it <i>is</i> a failing leads to some healthy debate, and shows me how Python developers take care with what they add to the language knowing that it is far harder to remove something that proves to be a mistake. It is right that such proposals should be debated and the onus is on those who wish to make a change to prove their change would be advantageous. I personally think Python gains from having a smaller syntax in general - I am one of those Python users who use several other languages as well as Python, but prefer Python for a lot of my personal projects. Switching languages is helped by Python being readable and concise, so I tend to be cautious about new additions. On comp.lang.python, those answering questions are proud of their language and I guess it may show. But their is also a very strong wish to be helpful and to be polite as well as to preserve c.l.p as a good place for people to get help. I am sure if you posted examples of c.l.p being "pretty neat" to the newsgroup, that it would be read. If you want to improve Python by giving it a switch statement then you might try trawling the developers mailing list as well as c.l.p. and seeing how you can advance the argument. (One of the more persuasive arguments I have seen is for proponents of change to trawl the standard library and show how "better" it could be with the new feature using it for examples and statistics, as well as doing an implementation, so people can try it out). - Paddy. Hi ale,
I do not think the lack of an explicit switch statement is a major failing of Python. Whether it is a failing leads to some healthy debate, and shows me how Python developers take care with what they add to the language knowing that it is far harder to remove something that proves to be a mistake.
It is right that such proposals should be debated and the onus is on those who wish to make a change to prove their change would be advantageous.
I personally think Python gains from having a smaller syntax in general – I am one of those Python users who use several other languages as well as Python, but prefer Python for a lot of my personal projects. Switching languages is helped by Python being readable and concise, so I tend to be cautious about new additions.
On comp.lang.python, those answering questions are proud of their language and I guess it may show. But their is also a very strong wish to be helpful and to be polite as well as to preserve c.l.p as a good place for people to get help. I am sure if you posted examples of c.l.p being “pretty neat” to the newsgroup, that it would be read.
If you want to improve Python by giving it a switch statement then you might try trawling the developers mailing list as well as c.l.p. and seeing how you can advance the argument. (One of the more persuasive arguments I have seen is for proponents of change to trawl the standard library and show how “better” it could be with the new feature using it for examples and statistics, as well as doing an implementation, so people can try it out).

- Paddy.

]]>
By: ale http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-546 ale Sun, 10 Feb 2008 14:01:00 +0000 http://eikke.com/how-not-to-write-python-code/#comment-546 @Paddy I think you are exaggerating the switch statement semantics variability for effect. The variation is minor and it is only pertinent to the fall-through behaviour in C/c++/Java/php's. ruby, lisp, ml, haskell all share identical semantics, and in all of these cases, including the fall-through ones, the meaning is clear from a naive point of view since the semantics are _explicit_. (There's another pythonic argument for it) Do you honestly think that the dispatch method is as intuitive as a switch statement to a non-programmer? (If yes, then that is all you need to answer) I don't even think it is as intuitive to a python programmer since i've been a python programmer for a long time and i still can spot a switch statement's intentions much quicker, not to mention find the right method being called, since while python enforces indenting for standard blocks, it can do nothing about dictionary's contents. Note also that the list of tips is very generic and most would apply equally well to pretty much any other language, but definitely not the dispatch. The point about not carrying bad patterns from other languages is already explicited in another point. The dispatch hint just sits by itself, as a non-obvious solution to a problem that people will obviously want to solve. But my main objection was to the "pretty neat", which pervades the c.l.p newsgroup, exulting on the idioms that were invented to paper over the language's flaws. It's as if the 'if' statement wouldn't have been included, they would defend the use of the dispatch mechanism for this too. If the language can afford a 'with', it can definitely afford a 'switch' or a 'case' or a 'when' (i like when best). It is very far from bloated still. @Paddy
I think you are exaggerating the switch statement semantics variability for effect. The variation is minor and it is only pertinent to the fall-through behaviour in C/c++/Java/php’s. ruby, lisp, ml, haskell all share identical semantics, and in all of these cases, including the fall-through ones, the meaning is clear from a naive point of view since the semantics are _explicit_. (There’s another pythonic argument for it)

Do you honestly think that the dispatch method is as intuitive as a switch statement to a non-programmer? (If yes, then that is all you need to answer) I don’t even think it is as intuitive to a python programmer since i’ve been a python programmer for a long time and i still can spot a switch statement’s intentions much quicker, not to mention find the right method being called, since while python enforces indenting for standard blocks, it can do nothing about dictionary’s contents.

Note also that the list of tips is very generic and most would apply equally well to pretty much any other language, but definitely not the dispatch. The point about not carrying bad patterns from other languages is already explicited in another point. The dispatch hint just sits by itself, as a non-obvious solution to a problem that people will obviously want to solve.

But my main objection was to the “pretty neat”, which pervades the c.l.p newsgroup, exulting on the idioms that were invented to paper over the language’s flaws. It’s as if the ‘if’ statement wouldn’t have been included, they would defend the use of the dispatch mechanism for this too. If the language can afford a ‘with’, it can definitely afford a ‘switch’ or a ‘case’ or a ‘when’ (i like when best). It is very far from bloated still.

]]>
By: Paddy3118 http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-545 Paddy3118 Sun, 10 Feb 2008 12:48:54 +0000 http://eikke.com/how-not-to-write-python-code/#comment-545 Hi ale, Dispatch is handled by the dict method, there is no mention that it cannot do what switch might be used for in other languages. Switch semantics are far from uniform in other languages (I dislike those that allow the statements of one case to flow into those of the next without an explicit break type statement at the end of a case clause). Pythons function arguments are very rich and would allow some flexibility in function signature for the uniform dispatch used. The dict method is not obvious to those knowing languages that have switch statements - it is a Python idiom that needs to be learned, but a powerful idiom. The tips and tricks list is necessary to remind those4 new to Python , and especially those who program in other languages, that Python is best written using its own idioms rather than trying to use or adapt idioms from other languages. It is not as if the inclusion of a switch statement isn't discussed by the developers - it is, and maybe your comments were meant for that audience, but if you are going to add new statements to Python, I'd rather have a bigger payback for the resultant language bloat. - Paddy. Hi ale,
Dispatch is handled by the dict method, there is no mention that it cannot do what switch might be used for in other languages. Switch semantics are far from uniform in other languages (I dislike those that allow the statements of one case to flow into those of the next without an explicit break type statement at the end of a case clause).
Pythons function arguments are very rich and would allow some flexibility in function signature for the uniform dispatch used.
The dict method is not obvious to those knowing languages that have switch statements – it is a Python idiom that needs to be learned, but a powerful idiom.
The tips and tricks list is necessary to remind those4 new to Python , and especially those who program in other languages, that Python is best written using its own idioms rather than trying to use or adapt idioms from other languages. It is not as if the inclusion of a switch statement isn’t discussed by the developers – it is, and maybe your comments were meant for that audience, but if you are going to add new statements to Python, I’d rather have a bigger payback for the resultant language bloat.

- Paddy.

]]>
By: ale http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-541 ale Sun, 10 Feb 2008 11:14:16 +0000 http://eikke.com/how-not-to-write-python-code/#comment-541 @Paddy Python does remove lots of occurrences of the switch statement, but it still leaves a lot of them in there (enough for the dict method to make it into something like this list, and enough for me to have developed a disliking for it). More importantly, the fact that most leftover cases are large where whatever has to be called is big enough to deserve their own function is orthogonal to my objection, since even if you have to call a function, it is better to do it inside the switch because the code is semantically clearer (having a switch doesn't prevent the cases from being function calls). When any programmer sees a switch statement it is obvious what is going on. The dict method is not obvious. I even tend to write it in the if - elif - elif sometimes because of this. Note that it is perfectly possible to use the dict semantics in pretty much every other language, but it is rarely used outside of python, afaik. A less-important problem with the dict dispatch method is that all the functions being dispatched have to have the same signature, which either forces you to write functions just for the switch statement, regardless of whether they could have been reused from elsewhere, or use partials/currying and thus add another bit of trickiness for something that should be in the language in the first place. Finally, a switch statement to me seems more pythonic: there should be one obvious way to do it. The fact that someone felt that something as basic as the dispatch method needed to be added a tips-and-tricks list is a clear sign that it is failing the obvious criteria. @Paddy
Python does remove lots of occurrences of the switch statement, but it still leaves a lot of them in there (enough for the dict method to make it into something like this list, and enough for me to have developed a disliking for it).

More importantly, the fact that most leftover cases are large where whatever has to be called is big enough to deserve their own function is orthogonal to my objection, since even if you have to call a function, it is better to do it inside the switch because the code is semantically clearer (having a switch doesn’t prevent the cases from being function calls). When any programmer sees a switch statement it is obvious what is going on.

The dict method is not obvious. I even tend to write it in the if – elif – elif sometimes because of this. Note that it is perfectly possible to use the dict semantics in pretty much every other language, but it is rarely used outside of python, afaik.

A less-important problem with the dict dispatch method is that all the functions being dispatched have to have the same signature, which either forces you to write functions just for the switch statement, regardless of whether they could have been reused from elsewhere, or use partials/currying and thus add another bit of trickiness for something that should be in the language in the first place.

Finally, a switch statement to me seems more pythonic: there should be one obvious way to do it. The fact that someone felt that something as basic as the dispatch method needed to be added a tips-and-tricks list is a clear sign that it is failing the obvious criteria.

]]>
By: Issyk-Kul Karakol http://eikke.com/how-not-to-write-python-code/comment-page-1/#comment-540 Issyk-Kul Karakol Sun, 10 Feb 2008 10:06:51 +0000 http://eikke.com/how-not-to-write-python-code/#comment-540 Great tips! Thanks. I'm still beginner in Python but wrote a few scripts and your article is useful (Thanks for Pypi link - didn't know it before)! Great tips! Thanks. I’m still beginner in Python but wrote a few scripts and your article is useful (Thanks for Pypi link – didn’t know it before)!

]]>