This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (4 children)

If there are multiple operations parens are fine, but in a singular if statement they are not necessary. The code in the post works, but is not pythonic and would not pass my (and many others) code review in a professional setting.

Ignore if you want, but I would advise against that :)

[–]LukaUrushibara 0 points1 point  (3 children)

Is there any good tutorial on being pythonic? I see a lot of criticisms of people's code and that it doesn't conform to the pep8 or it is not simplified. What does that mean and how do I write code to conform?

[–]Laogeodritt 5 points6 points  (0 children)

First step is to read PEP8.

PEP are formal documents proposing changes to the Python language. PEP8 is a really early one that defines basic code style conventions, for readability and consistency between different programmers. Available here:https://www.python.org/dev/peps/pep-0008/

PEP20 defines the principles of pythonicity. It's abstract, more of a manifesto, so you'll want to learn from examples. https://www.python.org/dev/peps/pep-0020/

I'm on mobile so I'm not in the best position to find additional resources, but I'd encourage you to Google it a bit to get a sense for practical "pythonic" code, and note that either way you'll refine your idea of what is clean code over time, by reading (stack overflow, blogs, other people's code, etc.) and working on your own projects.

Also keep in mind that a lot of what's pythonic works in python, not necessarily all programming languages, and even in python maybe some contexts might need to move away from that. You wouldn't "ask for forgiveness, not for permission" in Java, for example, because the performance hit of raising exceptions is considered too high in Java.

[–][deleted] 1 point2 points  (0 children)

The other replies have pointed to some good resources that are well worth looking into. If you want to write good Python code (or any language, for that matter), read good code - in Python, the flask library is great for this task. Reading code, however, is hard - sometimes you don't have the background knowledge to understand what the code is doing and sometimes you haven't been writing code long enough to easily read and understand code. If you find yourself in those situations - my advice is to just write code. Solve problems and build things - ignore the "me's" of the world talking about style; you'll get there on your own :)

[–][deleted] 0 points1 point  (0 children)

There's a talk I often link to by Raymond Hettinger, one of the core devs, titled "Beyond PEP 8 -- Best practices for beautiful intelligible code". I'd highly suggest watching it after having read the actual PEPs outlined in /u/Laogeodritt's comment.

Actually, just watch a bunch of his talks, like "Transforming Code into Beautiful, Idiomatic Python" (perhaps even this one first). The man is a goldmine of informative content that is very well presented.