python - Map attributes to classes both ways -


i have 2 classes, lower , higher1. when call parse method of instance of lower checks kind of higher class represents, in example show higher1. creates instance of higher class , returns it. can convert instance of higher class instance of lower calling it's build method.

class lower:      def __init__(self, data):         self.data = data      def parse(self):         if self.data[0] == "a":             value = self.data[1:]             return higher(value)         else:             ...   class higher1:      def __init__(self, value):         self.value = value      def build(self):         return lower("a" + self.value) 

so far good, want create classes every possible value of value attribute of higher1. assuming "green" , "blue" possible values there 2 additionally classes called higher1green , higher1blue.

higher1("blue") evaluate instance of higher1blue.

higher1("blue").build() evaluate instance of lower attribute data beeing "ablue".

my question if possible specifying mapping between example blue , higher1blue 1 time , not @ multiple locations in code. don't care if classes higher1blue , higher1green generated dynamically or defined using class.


my first approach overwrite __new__ method of higher1 able return instances of higher1blue or higher1green instead. couldn't find way backwards mapping (higher1blue.build()) without specifying mapping time.

using following link able achieve wanted:

reverse mapping class attributes classes in python

i didn't want vote duplicate because it's not same question , have taken long time wait 50 votes, time people spend time on finding solution when got one.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -