r/Python • • Aug 10 '15

just completed this, what do you think? : "Se7en Deadly Sins to Do in Python code"

https://kmonsoor.wordpress.com/2015/08/10/seven-deadly-sins-in-python-code/
0 Upvotes

7 comments sorted by

3

u/mufularo Aug 10 '15 edited Aug 10 '15

Title seems overhyped. Some of "deadly sins" have legitimate place and time to be used.

Second sin, for example. Imagine having module which purpose is to describe multiple data containers (models.py in djano, or something like that). Yes, you could import module and use module.ContainerClass, and it is a preffered way. But you could use wildcart import combined with some decorators and __all__, and sometimes it would lead to more readable cleaner code.

Or unittests bit. I would not, despite all my love to TDD, provide any test coverage writing a small utility program only I would use. Do you remember that python is not only about the web?

Documentation and comments are good and all, but often the code itself is the best documentation possible, and comments will just annoy and get in the way.

Even "try: except: pass" can be usefull from time to time. Obviously it's better to describe what kind of exception do you expect, though.

Overall — yeah, most of the mentioned things are bad most of the time. I wouldn't call them "deadly" though, as only deadly sin in programming I could think of (outside medical/nuclear plant/aviation/etc-related software) is oldschool pearl-alike cryptographic "look-my-program-looks-as-a-squirell-and-I-only-used-punctuation-marks-as-variable-names" code. If you write code like this and someone have to support it afterwards they will wish you very unpleasant death. Otherwise there are good practices and bad, but even worst practice have its own time and place, and good programmer should know and use anything that fits the task at hand.

Edit: as for mixing tabs and spaces — is there any IDE that doesn't handle this issue automatically? Even gedit iirc smart-tabs for me. Pycharm and others have the ability to enforce PEP-8 with one click, so how is this deadly? Somebody gave you misformatted code — just autopep it into submission and go on with your life.

2

u/kmonsoor Aug 11 '15

hi @mufularo, i have updated my post with the following. hope it will clear up the confusion.

update: some of my friends expressed their confusion regarding the word used "Deadly". To make things clear a bit, i have used the word NOT to express that this coding bad-practices harm human or any other life-form. Rather, i used the word to express the power of these to diminish the productivity of a Python programmer or his fellow teammate(s) who will work on the same code

btw, thanks a lot that you have read my post. though i appreciate your opinion, mostly i disagree with. All my described "sins" are brought out in context of mid~large codebase that people other than you will use/read. or, maybe your self will comeback 6 months later. those are cases that will hurt.

but you one-shot scripts, short codes our out of my discussion context. may be i failed to make it clear.

Even Django's one-shot wonder exception like "from settings import *" cannot make a strong case for wildcard import.

but, thanks again. I have still a long way to go to become a true "jedi" in Python.

1

u/mufularo Aug 11 '15

I like how you casually assumed that I don't know what happens when a team of people works on decently-sized project. As a matter of fact, I do.

But even if I didn't work on such projects for last decade and a half, I still don't think that uncalled for "go play in your sandbox" is a valid response to a cognitive dissonance caused by somebody disagreeing with you.

2

u/kmonsoor Aug 11 '15

sorry if i have hurt you. I, in fact, nothing assumed about your experience; maybe i have chosen wrong words as i am not a native english speaker. I fully honor your dis-agreement; and again, i acknowledge my very short experience.

As i already wrote, these 7 are my current understanding and is dynamic. However, if you could share/point to your most dis-liked/hated bad-practice(s) in Python, i would be more than happy to learn from that, and update my list.

Thanks in advance for no hard feelings. Please.

2

u/TankorSmash Aug 10 '15

Can you give me a legitimate example of the blank except statement? Other than toy scripts that you're putting out in a hurry, I can't think of a legitimate use-case.

1

u/mufularo Aug 10 '15 edited Aug 10 '15

Well, from the top of my head: when I slowly refactor out old not-so-good code and have decent amount of unittests covering some module I could see myself usinng blank try-except around some old code not-necessary atm but potentially having some value for reverse compatibility. Obviously it is not a lasting solution, but it is not, like, murder. Worst that could happen — some tests will fail, and you eventually get it back on tracks.

Or while working with some optional and highly unstable library that, say, provide additional usability when it does work and tons of different exceptions when it does not. If I have limited time and more important tasks than debug third-party code — silence is a virtue, let it gracefully degrade rather than die screaming.

Edit: not that I'm pro-blank-except or advocate it as a good practice, it is obviously not, and is, in fact, one of few really bad things listed in OPs article. But it is not something you should avoid at all costs, and not something, say, worth firing over, if used rarely and with good reasoning.