I'm trying to rewrite some javascript code into Python. Which brings some problems since I'm quite new to python but it's a good excercise I reckon.
Anyway, the problem at hand is that I need dynamic methods in an object that are defined somewhere else... (I know how this sounds, but bear with me for a sec)
Basicly there is a Tile object that can be several types of tile, but instead of making those types extent a main Tile object the choice has been made to put the functionality into a sort of array,
tileTypes = {
1: {
color: red,
func: dynamic_function(){
//code here
}
},
2: {
color: green,
func: dynamic_function(){
//other code here
}
},
}
var Tile = function(type)
{
this.color = tileTypes[type].color
this.func = tileTypes[type].func
}
(The real life code is much larger then this, but it serves it's purpose as an example)
I know this isn't the nicest code out there (it feels really weird working like this) but it is highly dynamic and new types can be added very fast so I forgive it.
However, I don't know how to build this in python.
NOTE: I'll probably won't actualy use it, and instead, use some kind of mapping from the type id's to classes or something, but I am very curious if it would be even possible to make it like that)