0

I have a BaseClass and two classes (Volume and testing) which inherits from the BaseClass. The class "Volume" use a method "driving_style" from another python module. I am trying to write another method "test_Score" which wants to access variables computed in the method "driving_style" which I want to use to compute further. These results will be accessed to the class "testing" as shown.

from training import Accuracy
import ComputeData
import model

class BaseClass(object):
    
    def __init__(self, connections):
        self.Type = 'Stock'
        self.A = connections.A
        self.log = self.B.log
        
    def getIDs(self, assets):
        ids = pandas.Series(assets.ids, index=assets.B)
        return ids
    
class Volume(BaseClass):
    
    def __init__(self, connections):
        BaseClass.__init__(self, connections)
        self.daystrade = 30
        self.high_low = True
    
    def learning(self, data, rootClass):
        
        params.daystrade = self.daystrade
        params.high_low = self.high_low
        
        style = Accuracy.driving_style()
        return self.Object(data.universe, style)
    
class testing(BaseClass):

    def __init__(self, connections):
        BaseClass.__init__(self, connections)
    
    def learning(self, data, rootClass):
        test_score = Accuracy.test_score()
        return self.Object(data.universe, test_score)
    
def driving_style(date, modelDays, params):
 
    daystrade = params.daystrade
    high_low = params.high_low
    
    DriveDays =  model.DateRange(date, params.daystrade)
    StopBy = ComputeData.instability(DriveDays)
    
    if high_low:
        style = ma.average(StopBy)
    else:
        style = ma.mean(StopBy)
    
    return style

def test_score(date, modelDays, params):
    "want to access the following from the method driving_style:"
    DriveDays = 
    StopBy = 
    
    return test_score ("which i compute using values DriveDays and StopBy and use test_score in the method learning inside
                       the 'class - testing' which inherits some params from the BaseClass")

1 Answer 1

0

You can't use locals from a call to a function that was made elsewhere and has already returned.

A bad solution is to store them as globals that you can read from later (but that get replaced on every new call). A better solution might to return the relevant info to the caller along with the existing return values (return style, DriveDays, StopBy) and somehow get it to where it needs to go. If necessary, you could wrap the function into a class and store the computed values as attributes on an instance of the class, while keeping the return type the same.

But the best solution is probably to refactor, so the stuff you want is computed by dedicated methods that you can call directly from test_score and driving_style independently, without duplicating code or creating complicated state dependencies.

In short, basically any time you think you need to access locals from another function, you're almost certainly experiencing an XY problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.