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.

13 Likes

Wouldn’t except be a better keyword?

4 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)

1 Like

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