Pythonic definition
Pythonic definition |
Pythonic means exploiting the features of the Python language to produce code that is clear, concise, and maintainable. Pythonic means code that doesn't just get the syntax right but that follows the conventions of the Python community and uses the language in the way it is intended to be used. The style at which code is written can also be interpreted as pythonic described in detail in PEP 8 style guide (too long to list here). Unpythonic code will run without a traceback. Examples of unpythonic code often come from users of other languages, who instead of learning a Python programming patterns such as list comprehensions or generator expressions, attempt to crowbar in patterns more commonly used in C or java. Loops are particularly common examples of this. In Java (similar with C/C++)
for i in (i; i < items.length ; i++)
In Python we can try and replicate this using while loops but it would be cleaner to use
for i in items:
Or, even a generator expression
(i.perform_action() for i in items)
Similar to above a common unpythonic approach would be to use range(len(sequence)) in python Other examples for unpythonic would be: using if conditions to mimc switch statements of other languages instead of using dictionaries, flooding namespace with * imports, or some may even include using the built-in global instead of using classes. So essentially when someone says something is unpythonic, they are saying that the code could be re-written in a way that is a better fit for pythons coding style. Code that is not Pythonic tends to look odd or cumbersome to an experienced Python programmer. It may also be overly verbose and harder to understand, as instead of using a common, recognizable, brief idiom, another, longer, sequence of code is used to accomplish the desired effect. Since the language tends to support the right idioms, non-idiomatic code frequently also executes more slowly. To be Pythonic is to use the Python constructs and datastructures with clean, readable idioms. It is Pythonic is to exploit dynamic typing for instance, and it's definitely not Pythonic to introduce static-type style verbosity into the picture where not needed. To be Pythonic is to avoid surprising experienced Python programmers with unfamiliar ways to accomplish a task. A Python-based framework can be considered Pythonic if it doesn't try to reinvent the wheel too much where there already language idioms to accomplish the same thing. It should also follow common Python conventions concerning idioms. Typing "import this" at the command line gives a summary of Python principles.
>>> import this |