Introduce 'exclude' keyword in python

Hello, Everyone

I have an idea to introduce exclude keyword in python example usage:

from os import * exclude (system, path, mkdir)

This should be appear in glob imports used for import everything to the current namespace excluding system and path and mkdir names

Hi, thanks for making this suggestion. It’s always good to hear new perspectives about how the language can evolve.

You haven’t explained what problem this change will solve. New keywords are difficult to add, so need very good justifications.

There’s much more about how to suggest changes in the devguide here: Changing Python.

2 Likes

So, you want to replace two lines of code

from os import *
del system, path, mkdir

by the following:

from os import * exclude (system, path, mkdir)

This does not seem like a very strong reason for adding another keyword.

5 Likes

Also, the import * form of import is pretty strongly discouraged in the first place, precisely because it introduces more names than you need. So adding syntax to patch over this problem seems like the wrong solution - instead, we should be encouraging people to explicitly import just the names they are using.

15 Likes

Wouldn’t except be a better keyword?

5 Likes

It’s like going to a restaurant and telling the waiter that you want everything on the menu except for those items you don’t want, which you then proceed to list…

3 Likes

Isn’t that like the Zen Buddhist Hot Dog Stand? You walk up and say “make me one with everything”…

5 Likes

If you go to an restaurant, you normally don’t order everything. You explicitly say what you need.

Per PEP-8 and the Zen, you should even avoid ordering everything, in favor of explicitly ordering only what you need.

1 Like

I’d like a burger with everything on except onion.

Now the waiter has to ask how you want you beef, what sauce you’d like, what kind of buns. Sure some things can be eaten together, but you don’t order a burger with everything and get multiple buns.

I however would like to order a burger with normal buns, cheese, salad, tomatoes, chicken instead of beef, burger sauce, and that’s it.

When are you going to run into a case, where you really need to import hundreds of things. I could imagine that happening with stubs, which can easily be generated, and with other API abstractions, which are usually generated too.

Maybe you can use an import_except function (name is misleading I know…), so that you can do

import typing

import_except(typing, "NoReturn", "Union")

x: Any # Works

That would mean that it is hard to know where stuff came from though, and linters /IDEs/ type-checkers will likely have a very hard time.

from burger import *
del onion

(though, more realistically, that analogy should correlate to creating your own instance of something)

2 Likes

The problem there is that you’ve been given onion, but thrown it away. You didn’t want to be given it in the first place!

At least I can see and quickly enumerate the likely toppings for the burger!

Get me one of everything in the department store downtown, except for a can opener – I already have one.

It’s not exactly the same as system, path or mkdir could exist before the import statement.

So it’s more like performing the * import and restoring excluded names to their previous values (if defined).

1 Like

Hold the pickles hold the lettuce
Special orders don’t upset us

1 Like

In practice, it doesn’t make much difference because modules are totally loaded in memory even when you try to get only one item, except for the fact that it adds the item to global scope and then deletes it.

Without any doubt.

Add new keywords is like a double-edged sword: you gain a new keyword, but lose a possible and important identifier name - break existent codes too.

Look this discussion for more details: Keyword Ambiguous Syntax


I understand the good intentions behind this feature, but I don’t think it would be a good idea because I wouldn’t know what’s being imported.

Adding keywords is a difficult tradeoff. But Pål was suggesting a keyword already in the language (except). You’re not losing any identifier names.

2 Likes

Can you give us a specific real-world example of an actual module from which you want to import all but a few names, in what context, and why importing those excluded names would cause problems for the rest of your code?

I don’t believe you have actual code that needs all names from the os module except system, path and mkdir. It just makes no sense.

1 Like

hypothetically, it might be useful if you’re used to importing * and you want to do something like

from math import *
from numpy import * except (floor, ceil)

here you can’t use the

from math import *
from numpy import *
del floor, ceil

trick, you’d have to write

from math import *
from numpy import *
from math import floor, ceil

I also hope I never encounter something like the above in the wild, and am very happy people have settled on import numpy as np to get all of numpy in scope :wink:

2 Likes