简体   繁体   中英

Python: how do you handle variables and modules of the same name?

I'd like to use a module and store the result of calling one of it's methods into a variable with the same name as the module, but that would overwrite the module. How to best handle this?

Example:

from something.deep.library import users
users = users.get_users()

Obviously, it's not the best practice to overwrite the library with a variable. How can I best handle this? I've tried many things: some are ugly and some don't work.

# doesn't work
from something.deep import library.users
users = library.users.get_users()

# ugly and inconsistent across the codebase
from something.deep.library import users as library_users
users = library_users.get_users()

Python best practices :

import library
users = library.users.get_users()

Using import - as notation is another good way, but imho first one is more clean and readable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM