简体   繁体   中英

Force use of module name before calling function, in Python

I'm creating a module that I'll import in a main script. In the module, called "colors", there's a function: "info()".

colors-module:

def info(function=None):
    print("\ncolors\n        Info\n")

The problem I have, is that I may also create a function called "info()", in the main script. This won't be a problem, as long as I import the colors-module as:

import colors

If so, I would call the function in the module by writing:

colors.info()

If I instead import the module as:

from colors import *

I have two functions called the exact same.

Main script:

from colors import *
def info():   # A
    print("Main script's function")

info()   # A
colors.info()   # Am I able to force use of the module name before calling the
                # function, if I import the module as in this script? Can this
                # be done from the module, and not from the main script? As said,
                # force use of module name, as "colors.info()", shall only apply
                # when the module is being imported with "from colors import *".

EDIT 1

The reason why I want to import the module this way, is because of global variables in it:

bwc0 = (0, 0, 0)
bwc1 = (1, 1, 1)
bwc2 = (2, 2, 2)
# All the way to 255

After a few answers, I'll try adding these to a class, and import that class as *, if possible. I also have a few functions in it, that I want imported as *, too. I'm not exactly sure how to import them, yet. It's probably easy, I suppose.

def redc(value):
    return value, 0, 0

def greenc(value):
    return 0, value, value

Thanks for all help

When importing * from the module you won't have two functions with the same name, you'll have one function with the name info . Which of the two (one in colors or one in main script) is used depends on where the definition of info in main is, relative to the from colors import * statement.

You can't "force" the module prefix in any way; if you didn't import colors with import colors you don't have that name bound to something you can refer to.

If typing long names is the thing you're trying to avoid, just rename the info function you bring in from colors by using the as clause of the from import statement:

from colors import info as c_info

This is one of the main reasons using star * imports are discouraged and why namespaces are such a good idea. With * you throw away the separate namespace (module colors ) that holds the name info function and place it in the namespace for the main script thereby masking objects with the same name.

No, you can't force how your function is called. If someone is writing code to call your function it's up to them to ensure that they are actually able to call it.

Either:

import colors

Or:

from colors import info as colors_info

will work if they want to call it. Otherwise they just have to avoid creating a function with a conflicting name.

BTW, as a general case don't use from colors import * as it means you don't know what function's you're importing. For example:

from colors import *
from foobar import *

now, is info() coming from colors or foobar ? You can't be sure, and somewhere down the line the answer may change. So always import the names you need explicitly and that way at least they're listed in the module that uses them so you can see when conflicts will arise.

You can make the colors like a class. And the function of info is method of colors class

so, you just instant of colors and call the info() method

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