Python Forum
calling an object variable in a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling an object variable in a dictionary
#1
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
Reply
#2
(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
99 percent of computer problems exists between chair and keyboard.
Reply
#3
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
Reply
#4
thanks the idea of putting the dictionaries in the __int__ fuction worked.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 746 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  Calling a base class variable from an inherited class CompleteNewb 3 1,591 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
Question Calling on a Variable using other Variables jacknewport 4 1,955 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  Defining an object's argument whose name is stored in a variable arbiel 2 2,144 Dec-11-2020, 10:19 PM
Last Post: arbiel
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,684 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  variable as dictionary name? diazepam 4 11,039 Apr-27-2020, 08:11 AM
Last Post: deanhystad
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,205 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  calling a variable in a for loop in windows dwaynes 1 1,774 Apr-02-2020, 05:21 PM
Last Post: mcmxl22
  zip file variable prints <zipfile.ZipFile object at 0x7f83fd13bd90> ? Rsh 9 4,005 Jun-27-2019, 02:00 PM
Last Post: Rsh
  Reference new dictionary keys with a variable slouw 4 2,836 May-07-2019, 03:30 AM
Last Post: slouw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020