Verbene
Verbene

Reputation: 1

How do I reset variables to standard values in Python

I am working on an application where variables get initialized to default values.

The user can change those values at any time. It should be possible for the user to reset some or all of the variables to default at any time.

How is the best way of going about this?

This would be a solution, but I have a feeling that it is suboptimal. Can you tell me if my feeling is correct and how I can do it better?

A_DEFAULT = "A_def"
B_DEFAULT = "B_def"
C_DEFAULT = "C_def"

class BusinessLogic_1(object):
    def __init__(self):
        self.setVariablesToDefault()
    def setVariablesToDefault(self, variableNames=None):
        # pass None to set all Variables to default
        variableNames = variableNames or ["A","B","C"]
        if "A" in variableNames:
            self.A = A_DEFAULT
        if "B" in variableNames:
            self.B = B_DEFAULT
        if "C" in variableNames:
            self.C = C_DEFAULT
    def printVariables(self):
        print  "A: %s, B: %s, C: %s" % (self.A, self.B, self.C)

if __name__ == "__main__":
    print "0: Initialize"
    businessLogic_1 = BusinessLogic_1()
    businessLogic_1.printVariables()

    print "Change A,B,C and then reset A,C"
    businessLogic_1.A = "A_new"
    businessLogic_1.B = "B_new"
    businessLogic_1.C = "C_new"
    businessLogic_1.printVariables()

Upvotes: 0

Views: 1503

Answers (2)

Dominic Ressel
Dominic Ressel

Reputation: 11

This might be a solution. It's a way to pass names of variables as strings and then to access them via these names. Objects can have more than one name simultaneously, but names always point to a single object (at a given point of time).

class classWithVariablesThatHaveDefaultValues(object):

    def __init__(self, some, example, variables, defaultValuesDict):
        self.some = some
        self.example = example
        self.variables = variables
        self.dictionaryWithDefaultValues = defaultValuesDict

    def resetValues(self, *listOfVariables):
        for variable in listOfVariables:
            for key in self.dictionaryWithDefaultValues:
                if key == variable:            
                    vars(self)[key] = self.dictionaryWithDefaultValues[key]

if __name__ == "__main__":

    defaultValuesDict = {"some":4, "example":5, "variables":6}
    exampleObject = classWithVariablesThatHaveDefaultValues(1, 2, 3, defaultValuesDict)
    exampleObject.some = 15
    print exampleObject.some, exampleObject.example, exampleObject.variables
    exampleObject.resetValues("example", "some")
    print exampleObject.some, exampleObject.example, exampleObject.variables

The Output is:

15 2 3

4 5 3

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142176

You can achieve it in a manner very similar to how decimal.localcontext() works, but depending on your use case, this may not be suitable. This will allow you to manipulate and call any methods on the original object reflecting the updated values, and at the end of the with block, reset them to the values upon entry.

from contextlib import contextmanager

class A(object):
    def __init__(self):
        self.a = 3
        self.b = 5
    def display(self):
        return self.a, self.b

a = A()

@contextmanager
def mycontext(obj):
    old_a = obj.a
    old_b = obj.b
    yield obj
    obj.a = old_a
    obj.b = old_b


print 'before:', a.display()
with mycontext(a) as obj:
    print 'entered:', a.display()
    a.a = 3
    a.b = 7
    print 'changed:', a.display()


print 'hopefully original', a.display()

before: (3, 5)
entered: (3, 5)
changed: (3, 7)
hopefully original (3, 5)

Upvotes: 0

Related Questions