Live data from Hacker News

Say “no” to import side‐effects in Python

chrismorgan.info

81–86 of 86 posts

Re: Say “no” to import side‐effects in Python

#81

Earlier quoted context omitted.

If you're on a Mac, good luck getting pip to install mysql-python. I use Macports for that.

This isn't a good solution. I pip install mysql-python several times a week on my mac ( I use different venv's for different branches and we have several different services that I work on in a given week, thus I'm installing packages via pip A LOT) and we always install it in a venv. Sure it can be a bit of a pain but it's well worth it IMO. Our steps to always get it working: make sure mysql_config is in your path e…

I couldn't ever get it to compile, but I haven't tried in a while. I don't actually need it much these days as I'm using Postgres for most things.

I'm sure there are other packages that are similar: not so easy to get working with pip but not impossible.

Re: Say “no” to import side‐effects in Python

#82

Earlier quoted context omitted.

In addition to the many other languages listed here, Go allows side effects on import.

That's interesting. I can see the motivation... it's helpful to have modules come into the world fully initialized, and initialization often involves side effects. Is Rust going to allow side effects on initialization?

No. Having module import perform side effects delays program startup unnecessarily and makes the semantics of the program depend on the order in which modules got initialized, which is confusing.

Re: Say “no” to import side‐effects in Python

#83

I am an author of a library [1] that does this. I understand the what the OP is talking about and agree with it, but like anything for me the rule is "(1) Don't do dangerous behavior X. (2) If you are an expert, do dangerous behavior X sparingly and with caution." Mind you, my library does not connect to a database, or any such crazy thing. It simply creates a singleton which is then useful throughout your applicatio…

Thinking about it, I do the same thing with my Ruby library mime-types[1]. The library is only useful if you load some data into its registry, so it does so automatically, unless you specify RUBY_MIME_TYPES_LAZY_LOAD in the environment.

I've just put myself a task to reverse the behaviour so that lazy loading is the default.

[1] https://github.com/halostatue/mime-types

Re: Say “no” to import side‐effects in Python

#84

Earlier quoted context omitted.

It's like programmers who find out about metaprogramming. Usually they grow out of it.

In Perl, I find metaprogramming to be extremely powerful. I don't go too deep into Class::MOP usually. However, I do find that focusing on tooling before code is usually a net win. This means a lot of work can be done by writing DSL's that use metaprogramming behind the scenes. It works well and has become a common approach for certain kind of things (like object frameworks).

I'm not saying you should never use metaprogramming. Sometimes there is no other (practical) way. But it can be easily abused, often for a little added convenience but a much larger conceptual complexity and additional debugging issues. Why have a well-designed API when you can have a metaprogrammmed mess instead?

Re: Say “no” to import side‐effects in Python

#85

Earlier quoted context omitted.

install my module! Also.. trust me because it's totally safe. curl http://foo.com/tBrwn | bash http://rvm.io/rvm/install

Out of curiosity, what would be better to handle the case of RVM? It's explicitly user space software, and unprivileged user space software at that, so having it require admin interaction (i.e. touching the system package manager) to install seems like a sledgehammer where a flyswatter would do. They could use a VCS repo of some kind, but that doesn't handle the various folder and script installs that need to happen…

being on github and using https doesn't guarantee anything, obviously.

If you look at the actual script it contains a ton of sudo and also additional curl commands. The chain of security is very lacking.

Re: Say “no” to import side‐effects in Python

#86

This in no way invalidates the point of the post, but I can't imagine going back to installing everything globally instead of using virtualenv. If you do that, at least you won't have every package you every looked at in your path.

I just can't get to use some modules within virtualenv. They seem to create more problems than using systemwide.

That I can remember, yep, PyQT and PySide, and Psycopg. Psycopg is not a problem on Linux, only Windows.
Post reply on HN