Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve a name clash between a defined function and a parameter in another function?

Tags:

python

I'm having a hard time figuring out the best way to resolve a name clash. Here's the gist of what I've got in front of me:

def clean():
    # do some cleaning stuff

def build(clean=True):
    if clean:
        clean()

Oops.

For a few reasons, I do not want to change the API here. What is the best strategy to resolve this conflict? For now, I'm doing:

def clean():
    # do some cleaning stuff
clean_alias = clean

def build(clean=True):
    if clean:
        clean_alias()

Which might be the best/only solution short of renaming things. I'm just wondering if there's a different way to reference the clean that's in the outer scope from within the body of the function?

like image 804
jsdalton Avatar asked Feb 01 '26 06:02

jsdalton


2 Answers

Try:

globals()['clean']()

globals()

Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

like image 71
Kos Avatar answered Feb 03 '26 19:02

Kos


Try adding a default argument, that binds the global clean function to a local variable.

def build(clean=True, cleanFn=clean): 
    if clean: 
        cleanFn() 
like image 24
PaulMcG Avatar answered Feb 03 '26 18:02

PaulMcG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!