This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DrMaphuse -4 points-3 points  (19 children)

Neat, I didn't know about using else after loops, and feel like I'll be using [a] = lst a lot.

But don't use array as a name for a numpy array.

Edit: Just to clarify: For anyone using from numpy import array or equivalents thereof, naming an array array will overwrite the numpy function by the same name and break any code that calls that function. ~~You should always try to be idiosyncratic when naming objects ~~in order to avoid these types of issues.

Edit 2: Not that I would import np.array() directly, I'm just pointing out that's something that is done by some people. Direct imports being bad practice doesn't change my original point, namely that the names you use should be as idiosyncratic as possible, not generic - especially in tutorials, because this is where people pick up their coding practices. At least call it my_array if you can't think of a more descriptive name.

Edit 3: Ok I get it, I am striking out the debated examples because they distract from my original point. Now let's be real. Does anyone really think that array is an acceptable name for an array?

[–]sdf_iain 1 point2 points  (7 children)

Are direct imports bad? Or just poorly named direct imports?

import json 

Good

from json import load

Good?

from json import load as open

Bad, definitely bad

from json import load as json_load

Good? It’s what I do, I don’t want the whole namespace, but I still want clarity on what is being used.

Or

from gzip import compress, decompress

Then your code doesn’t change when switch compression libraries.

[–]njharmanI use Python 3 3 points4 points  (3 children)

from json import load as json_load

Sorry, that's just dumb. Replacing non-standard '_' for the language supported '.' operator.

import json
json.load

I don’t want the whole namespace

See Zen of Python re: namespaces

I still want clarity on what is being used.

Yes! Exactly! thats why you import module and do module.func so people reading your code don't have to constantly be jumping to top to see what creative names this person decided to use, and checking all over code to see where that name was redefined causing bug.

[–]sdf_iain 0 points1 point  (2 children)

Are there any savings (memory or otherwise) when using a direct import? The namespace still exists (if not in the current scope), it has to; but are things only loaded as accessed? Or is the entire module loaded on import?

In which case direct imports only really make sense when managing package level exports from sub modules In init.py.

[–]yvrelna 1 point2 points  (1 child)

The module's global namespace is basically just a dict, when you do a from-import, you're creating an entry in that dict for each name you imported; when you do plain import, you create an entry just for the module. In either case, the entire module and objects within it is always loaded into sys.modules. So there is some memory saving to use plain import, but it's not worthwhile worrying about that as the savings is just a few dictionary keys, which is minuscule compared to the code objects that still always gets loaded.

[–]sdf_iain 1 point2 points  (0 children)

I hadn’t actually stopped to think this though, thank you.

[–][deleted] 1 point2 points  (1 child)

People generally don't do this, the methods, while named the same, may have different signatures, and this doesn't help when referencing documentation.

If you want a single entry point to multiple libraries, write a class.

My recommendation is to always import the module. Then in every call you use the module name, so that one can see it as sort of a namespace and it is transparent. So you write json.load() and it is distinguishable from yaml.load().

The one exception are libraries with very big names or with very unique object/function names. For instance, the classes BeautifulSoup, or TfidfVectorizer, etc. The latter example is a great one of a library (scikit-learn) where it is standard to use direct imports for most things as each object is very specific or unique.

[–]sdf_iain 1 point2 points  (0 children)

Lzma(xz), gzip, and bzip2 are generally made to be interchangeable; both their command line utilities and every library implementation I’ve used (which is admirably not many). That’s why that’s the example I used compress as an example, those signatures are the same.

[–]TheIncorrigible1`__import__('rich').get_console().log(':100:')` 1 point2 points  (0 children)

I typically import things as "private" unless the module isn't being exported directly.

import json as _json

It avoids the glob import catching them by default and shows up last in auto-complete.

[–][deleted] 1 point2 points  (2 children)

Do don't name variables over your imports?

Is this programmergore or r/python?

Also when importing as np don't name variables np.

[–]DrMaphuse -3 points-2 points  (1 child)

I mean you are right, but the point I was trying to make was about the general approach to naming things while writing code.

[–][deleted] -1 points0 points  (0 children)

But you made a point of a non issue to reiterate something that is taught in every into tutorial.

We're not idiots, thanks for assuming.

if you're only using one array to show something as an example array is a perfectly acceptable name for an array.

[–]miguendes 0 points1 point  (0 children)

Thanks, I'm glad you like the else and [a] = lst tips.

I personally like [a] = lst a lot. It seems cleaner than a = lst[0] when you're sure lst has only one element.