Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am trying to determine some best practices of function imports and PEP 8 does not explicitly speak about this, in fact the PEP 8 imports section is relatively small. I was wondering what the best practices were when importing using abbreviations.
For example:
import pandas as pd
import numpy as np
Are easily understood because they are very widely used packages. For my own case, I may have some obscure classes or functions in a module that I wish to import like:
from my_module import my_fun_function as mff
...
myvar = mff(input1)
versus
from my_module import my_fun_function
...
myvar = my_fun_function(input1)
Is there a best practices reference on this?
As you said. If the module is widely used it's acceptable to use abbreviations. If you have your custom module it is still okay to use abbrevation for it's name as long as it's well documented, clear and also widely used in code. Do not abbrevate if it's used just a couple of times.
However I would advise not to shorten function names as it feels like code obfuscation. I feel like ThisFunctionDoesThatThing(x) is way better than TFDTT(x).
I think it depends on your audience. If your audience is familiar with the function you are abbreviating or if you providing documentation for the function, then it certainly can make code more readable. However, if you abbreviate every single obscure function you import (even if you only use it once), then it becomes significantly less readable and a pain to understand.
There are no standards, for the aliases, although some are well accepted.
Keeping the alias short looks good most of the time, but is not the only option.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
For example,
import numpy
def text():
...
If there are a lot of import statements, I personally feel it seems better to import when it's needed, instead of importing all at the top of file. A major benefit of this is to make reader to understand the code easier and immediately know where the dependency is imported from.
Is there any side effect by doing this?
The ultimate guidelines for best practice in python are in the PEP-8 style guide. In the guide section linked:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
In your question, you assert:
A major benefit of this is to make reader to understand the code easier and immediately know where the dependency is imported from
But actually the opposite is often true. If I open a .py file, I can quickly scan the first few lines to see ALL dependencies needed to run the script. If each is imported below, then suddenly I have to scan through the whole file to see if and when a library is required.Remember that code is looked at a handful of times when it's written, and usually just by the author or someone close to the author, but could be read 10X more times, by people unfamiliar with the script
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
First, why there is no end in python? Second, the fact that there is tabs and there is no end make python the best readable/beautiful language, why they do not introduce these characteristics in other languages?
You'll have to ask Guido van Rossum about why he didn't introduce an end construct, since it's a language design issue - but as you say, there are aesthetic reasons not to do so.
The reason this isn't introduced to existing languages is that there are already billions of lines of code written in them, and you don't want to force people to change all of that just for some aesthetics.
Why not have it as a backward-compatible change, such as e.g. allowing languages with C-like syntax to have the opening { but not the closing }? Probably because programmers in those languages are very used to it and might actually prefer it to not having closing marks, and probably don't see this as a useful feature. Also, it would be necessary to make it a per-file decision, as a mixture of explicit and implicit block ends would be extremely confusing and probably not parseable.
As a matter of fact, Python itself contains a joke about this, which I believe reflects the authors' opinion on the matter:
>>> from __future__ import braces
File "<stdin>", line 1
SyntaxError: not a chance
(__future__ is a module which you can use to import certain pieces of functionality that were introduced in newer versions of Python and have been backported to older versions.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Can someone think of an example with good practice that uses exec?
If there is always a more efficient and secure way to replace exec, why python doesn't deprecate exec?
As explained in other questions, eval/exec are considered bad practice because they're generally abused to do a task where they aren't needed, leading to potential security issues and generally bad programming.
There are, however, valid uses for these mechanisms, and they expose important functionality that is not available elsewhere - executing arbitrary code at runtime.
Imagine, for example, that you want write a application that updates itself. You could fetch a script from a remote URL that runs with exec and updates your application to the latest version. While doing something like that, by itself, would pose a great security hazard, it's not hard to make the process secure through the use of digital signatures.
You can find another common use in the code module source: executing code input from the user at runtime for debugging purposes.
No, eval and related tools are not always bad by any measure.
There are a number of things that only work well when they are expressed as regular functions with regular, positional or keyword arguments (not magic *args or **keywords args). There's no way to dynamically create a function with a desired set of arguments, except with eval.
For a good example of how this can be used, examine the implementation of collections.namedtuple. Although it would be possible to create a class factory like that without eval, but all of the functions it defines, __new__ and _replace in particular, would have useless help text, and would be a less convenient tool without it. Worse, the non-eval implementation would almost certainly be SLOWER.
Another, more sweeping example of this exact use of eval is the fine decorator library, which generalizes this practice in a collection of tools that allow you to dynamically create functions with particular function signatures; it uses eval internally.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
This is a language design question. Why the designer didn't use
import A.B
instead of
from A import B
assuming A is a module that contains function B. Isn't it better to have a single style for import syntax? What was the design principle behind this? I think that the Java style import syntax feels more natural.
Python import statements primarily exist to load modules and packages. You have to import a module before you can use it. The second form of import is merely an additional feature, loading the module and then copying some parts of it into the local namespace.
Java import statements exist to make shortcuts to names loaded in other modules. Java import statements don't load anything, but merely move things into the local namespace. In Java you don't need to import modules in order to use them. The import statement has nothing to do with whether or not a module is loaded.
So the two languages take quite a different approach to importing. The imports statements are basically just not doing the same thing. Python's imports are for loading and Java imports are for shortcuts.
Java's approach would be somewhat problematic in python. In Java it's pretty easy to sort of what's a class/module/package from the syntax. Python does not have that advantage. As a result, the compiler and the reader would have difficulty determing what is and isn't meant to be a reference to an external package. For that reason, Python's designer chose to make it explicit and force you to specify which external module you want to load.
Consistency. import A.B never adds B to the local namespace, even for cases in which it is valid; it simply makes B available via A, which functions already naturally are.
You can do this with modules, but not functions, eg.
os.path is commonly imported like that
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Which is preferred
def method(self):
or
def method( self ):
With spaces in the parenthesis.
Check out PEP 8. It says to do the first one.
The common reference for Python style is PEP8, see: http://www.python.org/dev/peps/pep-0008/
To answer your question specifically, this is under "Pet Peeves":
Avoid extraneous whitespace in the following situations:
Immediately inside parentheses, brackets or braces.
Yes: spam(ham[1], {eggs: 2})
No: spam( ham[ 1 ], { eggs: 2 } )
There's PEP 8 the Python Style Guide:
http://www.python.org/dev/peps/pep-0008/
It suggests the former style, but note the introduction carefully.
I find the latter to be visual nails-on-chalkboard, personally.
http://www.python.org/dev/peps/pep-0008/
Python Style Guide
No space around the inside of function signatures. Occasionally I put space inside the parens of a function call if the arguments are particularly hairy, but never on the definition. I don't think any language makes a habit of that, actually.
I don't think you should blindly follow the PEP-8 style, like a vow upon a bible.
The PEP8 is intended for code that goes in the python standard library, i.e. the modules you can import just after you've installed Python, but they have very different development constraints than most of the software I write.
So I tend to think a lot about each "rule" (either from the PEP8, the pylint/pychecker ones, or the Google Style Guide) and see how it can apply to my code -- the pros and cons.
As for your question, people usually don't use spaces inside parentheses, although I've seen it in some code by coworkers that like it, IMHO it detracts a little from readability, but it's a minor issue.