Support for function importing via regex

Idea: from database_api import update_* 
Current: from database_api import update_success, update_failure, update_running, update_initialized

This could clean up some code bases by allowing users to regex match some functions when importing. Full Regex matching may be slightly difficult, as an MVP, support for asterisks (*) matching would go a long way.
In the above codeblock, this would import all functions which start with “update_” from the module database_api.

1 Like

I am completely against the idea, the first quality of Python is clarity of code and doing stupidly complicated things to make The International Obfuscated Python Code simple is The Wrong Thing™ to do (compare with IOCCC, also python3 -c 'import this').

Second, what you show is glob pattern (see glob package in the standard library), not regexp.

7 Likes

This idea probably won’t get much support, as the existing import * syntax is already discouraged. Part of the appeal of using the from module import stuff, things syntax is that it explicitly lists each name being imported; this proposal wouldn’t provide that explicitness.

Note that it’s already possible to avoid having to list all the names just by using qualified names, importing under an alias if you want to reduce keystrokes:

import database_api as dbapi

# later. . .
dbapi.update_initialized("Let's go!")

# later. . .
dbapi.update_success("We succeeded")

Personally I prefer this style as opposed to listing out all the names in the import statement.

4 Likes

Instead of filtering exported objects by names, a more modular approach is to adopt the pattern used by the asyncio package, where classes and functions are organized as sub-modules of the package, and all that the __init__.py of the package does is to star-import names from all the sub-modules and to re-export them by joining all their __all__ lists:

So that if one wants to “filter” all the transport protocols for example they can star-import them from the protocols sub-module:

from asyncio.protocols import *

You can similarly group your update_* functions into a sub-module named database_api.update instead.

2 Likes

@Niio Thanks for sharing your idea!

Whereas I don’t think it would be a good fit for the language itself (for similar reasons for which from x import * is discouraged in production code), I believe something along these lines might be a nice addition to the Python REPL – not as a syntax extension per se, but a kind of… action shortcut?

(It could be triggered by a sequence such as import identifier suffixed, as proposed, with asterisk… Or perhaps it could be just an optional action, with a dedicated keyboard shortcut, made available when tab-completion generates multiple options?)

Setting aside the reasons I think this is unsuitable for the language itself, and just focusing on the REPL, two things come to mind:

  1. Code accepted in the REPL should be valid in the language. Tab expansion is okay, raw input is not.
  2. Before adding something fancy to the REPL, do any editors do this?

I find it odd to presume that names which are related by a pattern are also related in activity or usage, although I’m sure it’s true in some cases.