Henry Smith
Henry Smith

Reputation: 35

Python Linking Variable Values

I want to to store a reference to a class object inside a dictionary or another class that will maintain the same value as the actual class object. Currently, I'm using a string of the class object and the eval() function. I know this isn't a proper solution but haven't found another fix.

wishLists.append(ListSetting("curWeapon.pveFlag or not curWeapon.pvpFlag", a, b, c, d))

My only idea is making a new function that has separates the boolean expression from a,b,c.. when making the ListSetting and adding that separately. Although I'm not sure if wishLists would update the firstFlag, secondFlag... variables.

firstFlag = ListSetting(a,b,c,d)
wishLists.append(firstFlag)

def wishListFlags():
    firstFlag.flags = curWeapon.pveFlag or not curWeapon.pvpFlag
    secondFlag.flags = ""
    ...

I'm pretty sure that updating the index of wishLists would work but would need a bunch of if statements or a dictionary.

firstFlag = ListSetting(a,b,c,d)
wishLists.append(firstFlag)

flagExpressions = {
    1 : curWishListcurWeapon.pveFlag or not curWeapon.pvpFlag,
    2 : "",
    ...}

def wishListFlags():
    for index in len(wishLists):
        wishLists[index].flags = flagExpressions.get(index)

If anyone knows a better way to go about this please let me know. Also, if my examples aren't specific enough or are confusing I'd be happy to share my entire program, I didn't know if it would be too much.

Upvotes: 0

Views: 40

Answers (1)

Barmar
Barmar

Reputation: 781974

To store an expression you use a function, which you later call to get the value of the expression.

flagExpressions = {
    1: lambda: curWishListcurWeapon.pveFlag or not curWeapon.pvpFlag
    2: lambda: ""
}

def wishListFlags():
    for index in len(wishLists):
        wishLists[index].flags = flagExpressions.get(index, lambda: None)()

Upvotes: 1

Related Questions