0

How to make an object generator

I'm trying to make a map for a dnd bot. My plan is to make a xy grid of objects that I can store data for each space in, and then later map it out (already figured out in another code). I've been butting my head in with this and can't quite get things worked out, as the code will either print out an error, or a None. https://code.sololearn.com/c0QqNFToX0tj/?ref=app How would I get something like this to work.

1st Jan 2020, 4:00 AM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
25 Antworten
+ 3
You want to calculate a value from strings. For that you need eval, not exec
1st Jan 2020, 5:51 AM
HonFu
HonFu - avatar
+ 2
Yep, switching the exec to eval on print did it. I wasn't familiar with eval prior to this and just thought of exec as a str or int for "raw text" (the text that's needed for naming variable and actual code) Thanks
1st Jan 2020, 5:58 AM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
+ 2
exec executes code, for example some print statement. eval calculates an expression. So basically, when you want to have a value, take eval. (And if you can prevent it, best use neither - it's not the safest tool in the box.)
1st Jan 2020, 6:03 AM
HonFu
HonFu - avatar
+ 1
Understood. But how exactly is it unsafe, besides having an open input into the exec/eval or potential errors if you do it wrong. Cause I'll probably have to use it fairly often to turn strings into variable names and similar
1st Jan 2020, 6:31 AM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
+ 1
Let me update weapon forge code, so I can show you
8th Jan 2020, 8:27 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
+ 1
Why do you have to have a variable name for a created weapon? Why this line with exec? Why don't you just, for example, create a dictionary d = {} and go like: d['Whatever'] = weapon(bla, bla... )
8th Jan 2020, 9:34 PM
HonFu
HonFu - avatar
+ 1
Then you can just do show(d[thatname])
8th Jan 2020, 9:41 PM
HonFu
HonFu - avatar
+ 1
Ok, I get what you're saying. But I'm not going to update the weapon forge, since that's just an example. It just slipped my mind that dictionaries could use strings as keys, and I was in the middle of typing the long comment when you sent that, so I didn't see your dictionary suggestion before I posted it. So, is there any risk to the eval and exec in the "working with classes" code (the original question)(ie, using exec and eval separate from any input)? If not, I think that's it, and thanks for all the help
8th Jan 2020, 10:16 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
Usually there are simple ways to convert strings to values without using eval or exec. Do you have a situation in mind where you would think you have to use eval?
1st Jan 2020, 8:37 AM
HonFu
HonFu - avatar
0
I think my main one is conversion for variable names, either in the creation or calling of a var. I'm not exactly sure what else I'll need as I'm learning as I'm going (I got my basics covered though). I just know that turning input into that "raw text" will definitely come in handy. I'll write some example code for the situation I explained
1st Jan 2020, 3:17 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
https://code.sololearn.com/cUT1zKW20oHX/?ref=app Here, I need exec and eval to turn strings into "raw text" to make and call variable names
1st Jan 2020, 5:15 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
HonFu I would also need exec and eval if I were to write more code, to make it where people can make weapons based on input
8th Jan 2020, 8:05 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
You should usually try your best to do without eval and exec. It's generally possible and usually not hard to find a way.
8th Jan 2020, 8:12 PM
HonFu
HonFu - avatar
0
So what are suitable replacements for eval and exec?
8th Jan 2020, 8:15 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
That depends on what exactly you want to do. exec and eval should only be used in very controlled situations. Simply spoken, your user could write 'delete hard-drive's content'. What if you exec that?
8th Jan 2020, 8:17 PM
HonFu
HonFu - avatar
0
For calling variable names, I can write an if statements making sure the input matches one of the variable names, else output invalid var name. However I'm not exactly sure what to do when creating variable names and such with input
8th Jan 2020, 8:26 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
Why do you need to do that?
8th Jan 2020, 8:26 PM
HonFu
HonFu - avatar
0
Done, though you'll need to run it elsewhere because sololearn makes an eof error if you do more than one input
8th Jan 2020, 9:07 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
I believe the code is safe enough, except the name, I'm not sure what to do there
8th Jan 2020, 9:08 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar
0
https://www.sololearn.com/post/214561/?ref=app Here's the output I got from it
8th Jan 2020, 9:11 PM
- TheLongTimeFan -
- TheLongTimeFan - - avatar