Returning different values based on where function is called from in Python
Question: I have two functions, each of which call a third function, but expect a
slightly different thing returned back.

All functions are in separate files. Files A and B can NOT import one another,
so function A and B can not call one another. What is the best way to organize
this?

I would like to avoid duplicating the function, since most of it is identical,
to stay in line with DRY principals.

Here is some pseudo-code for what I have, and how I am thinking the program
would be structured:

    
    
    #FileA.py
    def functionA():
        useableData = functionC(rawData)
    

-
    
    
    #FileB.py
    def functionB():
        useableData = functionC(rawData)
    

-
    
    
    #FileC.py
    def functionC(rawdata):
        *Extensive processing/formatting of data*
        if C run from A:
            return useableData
    
        if C run from B:
            *A bit more processing of data*
            return useableData
    

The only solution I can think of is to pass a second variable along with
`rawdata`, a string that says the name of the function it came from. But this
seems inelegant to me.

Is there an ideal way to do this? Perhaps not involving checking where the
function was called from, but still allowing `functionC` to return slightly
different results depending on where the data is going to be returned to. I
might be able to restructure the code a bit, but all three functions need to
remain in their current files.


Answer: I think your approach is wrong. What I would do is this

    
    
    def functionA():
        useabledata = functionC(rawData)
    
    
    def functionB():
        useabledata = functionD(functionC(rawData))
    
    
    def functionC(rawData):
       *Extensive processing/formatting of data*
    
    def functionD(partiallyProcessedData):
       *A bit more processing of data*
    

Other option is as tobias_k said, to have an optional second parameter

    
    
    def functionC(rawData, moreProcessing=False):
        *Extensive processing*
        if not moreProcessing:
            return data
        *more processing*
        return data
    

And call it like

    
    
    def functionA()
        useabledata = functionC(rawData)
    
    def functionB()
        useabledata = functionC(rawData, True)
    

An even more encapsulated way to do it would be

    
    
    def functionA():
        useabledata = functionC(rawData)
    
    
    def functionB():
        useabledata = functionD(rawData)
    
    
    def functionC(rawData):
       *Extensive processing/formatting of data*
    
    def functionD(rawData):
       partialProcessedData = functionC(rawData)
       *A bit more processing of data*
    

