Triple-double quote v.s. Double quote

From the PEP8 Style Guide: PEP 257 describes good docstring conventions. Note that most importantly, the “”” that ends a multiline docstring should be on a line by itself, e.g.: “””Return a foobang Optional plotz says to frobnicate the bizbaz first. “”” For one liner docstrings, it’s okay to keep the closing “”” on the … Read more

How can I use setuptools to generate a console_scripts entry point which calls `python -m mypackage`?

How can I use entry_points to generate a binary that calls python -m mypackage (and passes *args, **kwargs) ? I think this is the wrong way to look at the problem. You don’t want your script to call python -m mypackage, but you want the script to have the same entry point as python -m … Read more

What’s the difference on docstrings with triple SINGLE quotes and triple DOUBLE quotes?

No. They are the same. The only difference is that the first one can contain a sequence of three unescaped double quotes, while the second can contain a sequence of three unescaped single quotes. (In other words, because the delimiters are different, there is a slight difference in what characters you can use inside them.) … Read more

What PEP 8 guidelines do you ignore, and which ones do you stick to? [closed]

PEP8 says to avoid “More than one space around an assignment (or other) operator to align it with another” and “never use more than one space” around math operators, but I don’t follow this. I often add “extraneous whitespace” when neighboring lines are related or very similar, but not quite the same: search_start = (f … Read more

Better to ‘try’ something and catch the exception or test if it’s possible first to avoid an exception?

You should prefer try/except over if/else if that results in speed-ups (for example by preventing extra lookups) cleaner code (fewer lines/easier to read) Often, these go hand-in-hand. speed-ups In the case of trying to find an element in a long list by: try: x = my_list[index] except IndexError: x = ‘NO_ABC’ the try, except is … Read more

Data Classes vs typing.NamedTuple primary use cases

It depends on your needs. Each of them has own benefits. Here is a good explanation of Dataclasses on PyCon 2018 Raymond Hettinger – Dataclasses: The code generator to end all code generators In Dataclass all implementation is written in Python, whereas in NamedTuple, all of these behaviors come for free because NamedTuple inherits from … Read more

tech