Parentheses can and should be used to make them less ambiguous.
This is for the sake of people who read the code, even if it doesn't matter to the Python parser. Where you use lambdas and dictionaries in code, they should use the same style as their repr note the lack of spaces before the colons :. When testing for None , use is. This is because None is a singleton and the identity test is more efficient than testing for equality.
Where what you really mean is an identity rather than equality test then is can also make your code more clearly convey its intent. If a class has no base classes, then it is better to make it a new style class and inherit from object. This means you mustn't use mutable objects like dictionaries or lists as default arguments. No bare excepts.
If you really want to catch all exceptions, then use except Exception: , but it is many times better to only trap exceptions that you expect. Doing otherwise will suppress a multitude of bugs. Don't do it! Python indentation gives you a visual indication of structure; putting multiple statements per line breaks this.
Python Style Guide
I find all of the following ugly. Even for importing a lot of names it is better to be able to see where your names come from.
Tools like pylint and PyFlakes can help warn you about unused imports. Where you are importing lots of names, and targeting Python 2. There are various Python naming conventions I use.
Consistency here is certainly good as it helps to identify what sort of object names point to. I think the conventions I use basically follow PEP8. Module names in CamelCase with a main class name identical to the module name are annoying. ConfigParser , which should always be spelt configobj. Also , variables, functions, methods and classes which aren't part of your public API , should begin with a single underscore.
And finally, you should always have whitespace around operators and after punctuation. The exception is default arguments to methods and functions. For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store.
It is for Python and. NET programmers interested in making the most of Python on the. NET framework and Mono. Where relevant also indicate the default value: optional, default: 5. You can also use a little inline reStructuredText markup in docstrings, e.
CKAN datasets used to be called packages and the old name still appears in the source, e. When documenting functions like this write dataset not package, but the first time you do this put package after it in brackets to avoid any confusion, e. There are various tools that can help you to check your Python code for PEP8 conformance and general code quality. We recommend using them.
Python Style Guidelines - The Chromium Projects
Instead list the names you need explicitly: from module import name1 , name2. So: All modules and all public functions, classes and methods exported by a module should normally have docstrings see PEP Keep docstrings simple: use plain, concise English.
Try to avoid repetition. CKAN docstrings deviate from PEP in a couple of ways: We use '''triple single quotes''' around docstrings, not """triple double quotes""" put triple single quotes around one-line docstrings as well as multi-line ones, it makes them easier to expand later We use Sphinx domain object cross-references to cross-reference to other code objects see below We use Sphinx directives for documenting parameters, exceptions and return values see below.
Package objects '''.
Python Code Conventions
By default only free tags tags which do not belong to any vocabulary are returned. Tag objects '''. You must be a sysadmin to create vocabularies. As mentioned above, only perform style clean-ups on master to help avoid spurious merge conflicts. We try to stick to using single quotes, except in cases where using double quotes is more readable. For example:. It seems the preference is for slashes, but using parentheses is okay too. There are good arguments either way. Arguing about it seems like a waste of time. For the rationale, see PEP