Page 1 of 1

calling a dynamic variable within a class function

Posted: Fri May 06, 2022 2:22 pm
by Theh0llowone
Hi, this is my first post here so I'm sorry if I screw up any rules/formatting,
I'm also sorry if the title is unclear as to what I'm asking, but even I don't know what to call it,
What I generally want to know is if there is a way to have the variables in a function of a class be changed by the input, for example having something along the lines of info.update (Info, name of the variable).
Just to be able to not have to have a function to update each individual variable.
Thank you for taking the time to read about my issue.

Re: calling a dynamic variable within a class function

Posted: Fri May 06, 2022 2:59 pm
by RicharDann
I don't fully understand your question, but if you want to change the value of an object property, you can just assign the new value with an equal sign (=):

Code: Select all

init python:

    class Person():

        def __init__(self): 

            self.name = "Unknown"

default john = Person()

label start:
    "Default name: [john.name]"

    $ john.name = "John Doe"

    "New name: [john.name]"

Re: calling a dynamic variable within a class function

Posted: Fri May 06, 2022 4:57 pm
by Theh0llowone
Ahh, sorry my explanation wasn't the greatest, thanks for trying to answer anyway, I'm trying to see if it's possible to have one function that takes two arguments, one that is the information to be stored, and the other being the variable to store it in, for example, something like

Code: Select all

class LoveInterest(object):
        def __init__ (self, Affection, Friendship):
            self.Affection = Affection
            self.Friendship = Friendship
       
        @property
        def UpdateInfo(self, info, variablename):

Re: calling a dynamic variable within a class function

Posted: Fri May 06, 2022 10:10 pm
by Ocelot

Code: Select all

class Test:
    def update_info(self, variable, value):
        setattr(self, variable, value)

# . . .
$test = Test()
$test.update_value("level", 555)
"Level is :[text.level]"