6 ms·
What's common isn't necessarily a good practice to follow - https://google.github.io/styleguide/pyguide.html#Imports https://google.github.io/styleguide/pyguide
by dolinsky 10y ago
What's common isn't necessarily a good practice to follow - https://google.github.io/styleguide/pyguide.html#Imports https://google.github.io/styleguide/pyguide.html#Imports
By importing a function and assigning a 1 char alias to it, the readability of the code is impacted.
- pfranz 10y agoI'm a bit confused by your comment. From your link: > Use from x import y as z ... if y is an inconveniently long name. "import ... as" is both common and per Google's style guide a good practice to follow in this scenario.
- dragonwriter 10y agoNo, the Style Guide says "use imports for packages and modules only", which (alias or not) rules out importing single functions.
- pfranz 10y agoYou're right. I wouldn't import a function as a single letter module if I was only using it once or twice, but I do think there's value in some cases. decimal.Decimal("1.2345") or even d.Decimal("1.2345") can be cluttering. Going by the guide, you would do 'import pprint; pprint.pprint("foo")'...which, at least in my experience, I've never seen. It's always "from pprint import pprint" or "from pprint import pprint as pp" It could be that I work with slobs.
- dragonwriter 10y agoI don't think having a different view on the relative value of conciseness vs. visible name spacing than is at play in the Google Python style guide makes you a slob.
- Godel_unicode 10y agoYou find "decimal" to be an inconveniently long name? In my experience "import...as" is reserved for things like PowerIterationClustering and the like. The overriding principle is to always choose readability over succinctness.
- pfranz 10y agoDepends how often it's used. If you're just calling it once I wouldn't bother, but I was going off of makecheck's comment of, "if I had to write tons of those in code." I misread the style guide linked, I do find decimal.Decimal("12.34567") inconveniently long--but I generally don't mind inconveniently long names for increased intelligibility and the IDE does most of the heavy lifting. I don't often use "as" it's usually to increase the readability. Similarly, I often see "from pprint import pprint as pp"
- JimmyAustin 10y agoWhile I haven't used it with something as rarely used as Decimal, I almost always alias numpy and tensorflow to np and tf respectively. I find it cleans up the code drastically.
- fletchowns 10y agoThose are common conventions for those particular libraries though.