Python Forum
calling an object variable in a dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: calling an object variable in a dictionary (/thread-1361.html)



calling an object variable in a dictionary - sunhear - Dec-27-2016

my code in python 2.7 is:
class char(object):
    'chactors'
 
    #Create instances of object 
    def __init__(self,name,d):
        self.name = name
        self.d = int(d)
 
    def action(self,skill):
        for w in char.statlist: #run through statlist dictionary
            if skill == w: #refer to the value of the key
                print w # check the value of w
                sat = char.statlist[w] #find the value in statlist dictionary.
                print sat #check the value of dictionary value
                for q in char.satmap:
                    if q == sat:
                        sat = char.satmap[q]# set sat to char.d ( the value set in statlist)
                        print sat
                    else:
                        pass
 
                    for z in char.skillmap:
                        if z == skill:
                            skillint = char.skillmap[z] # set skillint to char.mov
                            print skillint
                        else:
                            pass
 
                t = skillint + sat
                print t #check to see if it ran as exepted
            else:
                print 'pass'
                pass
 
    statlist = {'mov':'d','act':'d'}
    skillmap = {'mov':'char.mov'}
    satmap = {'d':'char.d'}
 
 
char1 = char('q','1')
setattr(char1,'mov', 10)
setattr(char1,'act', 200)
 
char1.action('mov')
Currently it returns: char.movchar.d However I want it to add the 2 variables together to get 11. char.d/char.mov and self.d/self.mov do not work.Inside the quotes they are treated as string like the current script shows. Outside of  the quotes it gives a name recognition error.

What I need to know is how to reference a object variable in dictionary in the class of the object. This needs to work on mutable objects. What ever syntax is entered for the dictionary value needs to work when action is called on for another object.

Basically I... need these
skillmap = {'mov':'char.mov'}
satmap = {'d':'char.d'}
to refer to these
def __init__(self,name,d):
    setattr(char1,'mov', 10)
so these values are used
char1 = char('q','1')
setattr(char1,'mov', 10)
to produce this result
11
sunhear

p.s. sorry if the code tags show


RE: calling an object variable in a dictionary - Windspar - Dec-29-2016

(Dec-27-2016, 09:50 PM)sunhear Wrote: my code in python 2.7 is:
class char(object):
    'chactors'
 
    #Create instances of object 
    def __init__(self,name,d):
        self.name = name
        self.d = int(d)
 
    def action(self,skill):
        for w in char.statlist: #run through statlist dictionary
            if skill == w: #refer to the value of the key
                print w # check the value of w
                sat = char.statlist[w] #find the value in statlist dictionary.
                print sat #check the value of dictionary value
                for q in char.satmap:
                    if q == sat:
                        sat = char.satmap[q]# set sat to char.d ( the value set in statlist)
                        print sat
                    else:
                        pass
 
                    for z in char.skillmap:
                        if z == skill:
                            skillint = char.skillmap[z] # set skillint to char.mov
                            print skillint
                        else:
                            pass
 
                t = skillint + sat
                print t #check to see if it ran as exepted
            else:
                print 'pass'
                pass
 
    statlist = {'mov':'d','act':'d'}
    skillmap = {'mov':'char.mov'}
    satmap = {'d':'char.d'}
 
 
char1 = char('q','1')
setattr(char1,'mov', 10)
setattr(char1,'act', 200)
 
char1.action('mov')
Currently it returns: char.movchar.d However I want it to add the 2 variables together to get 11. char.d/char.mov and self.d/self.mov do not work.Inside the quotes they are treated as string like the current script shows. Outside of  the quotes it gives a name recognition error.

What I need to know is how to reference a object variable in dictionary in the class of the object. This needs to work on mutable objects. What ever syntax is entered for the dictionary value needs to work when action is called on for another object.

Basically I... need these
skillmap = {'mov':'char.mov'}
satmap = {'d':'char.d'}
to refer to these
def __init__(self,name,d):
    setattr(char1,'mov', 10)
so these values are used
char1 = char('q','1')
setattr(char1,'mov', 10)
to produce this result
11
sunhear

p.s. sorry if the code tags show

maybe this
class CharFactory(object):
    def __init__(self, name, d):
        self.name = name
        self.d = int(d)
        
    def action(self, attr, skill, value):
        if skill == 'mov':
            try:
                setattr(self, attr, getattr(self, attr) + int(value))
            except:
                print "Attribute ( {} ) doesn't exist in object".format(attr)
                
        elif skill == 'act':
            setattr(self, attr, int(value))
            
    def __repr__(self):
        return "CharFactory(name: {}, d: {})".format(self.name, self.d)

char3 = CharFactory('q','1')
char3.action('d', 'mov', '10')
char3.action('e', 'mov', '10')
print char3



RE: calling an object variable in a dictionary - Larz60+ - Dec-29-2016

I'm not sure what you are trying to do, but think it's something like this:
(this done in Python 3.5, can't stand 2.7 it's toooooo old
class char(object):
    'chactors'

    # Create instances of object
    def __init__(self, name, d):
        self.name = name
        self.d = int(d)
        self.statlist = {'mov': d, 'act': d}
        self.skillmap = {'mov': 'self.mov'}
        print('self.statlist: {}'.format(self.statlist))
        print('self.skillmap: {}'.format(self.skillmap))
        self.satmap = {'d': 'self.d'}
        self.skillint = None

    def action(self, skill):
        if skill in self.statlist:
            print('self.statlist[skill]: {}'.format(self.statlist[skill]))

def main():
    char1 = char('q', '1')
    char1.statlist['mov'] = 10
    char1.statlist['act'] = 200

    char1.action('mov')
    char1.action('act')

if __name__ == '__main__':
    main()
results:
Output:
self.statlist: {'mov': '1', 'act': '1'} self.skillmap: {'mov': 'self.mov'} self.statlist[skill]: 10 self.statlist[skill]: 200 Process finished with exit code 0



RE: calling an object variable in a dictionary - sunhear - Dec-30-2016

thanks the idea of putting the dictionaries in the __int__ fuction worked.