python - Does instantiating a class redefine the class including all of its methods? -
i'm making program go through @ least 1,016,064 gear permutations on diablo 3. i've been experimenting different theoretical implementations , decided wanted use classes represent each permutation rather having deal massive , convoluted dictionaries. method can store instance , replace when new permutation superior former.
in case takes computer (i7-3632qm) 40 seconds go through of permutations doing 30 flops per permutation, , cant imagine how long it'll take if has define 50 methods each time class instantiated. anyway, think it'll like:
class perm: def __init__(self, *args): self.x = 10 self.y = 5 self.z = 100 item in args: if hasattr(self, item): getattr(self, item)() self.val_1 = self.x * 2 self.val_2 = self.y * 5 self.val_3 = self.z/(self.z+300) def head_1(self): self.x += 5 self.z + 200 def head_2(self): self.x += 10 self.y += 10 def feet_1(self): self.y += 5 self.z += 250 def feet_2(self): self.x += 10 self.z += 500 current_best = perm('head_1','feet_2')
it seems correct way make objects each of gear options have, function calculates them all.
import itertools class gear(object): def __init__(self, *args, **kwargs): # have no idea gear should do... class headpiece(gear): ... class legs(gear): ... # etc def calculate_perm(gear_tuple): result = do_some_calculation_over(gear_tuple) return result best = max(itertools.permutations(all_your_gear), key=calculate_perm)
you create 1 class that's analogous perm
, though i'd give more descriptive name:
class equipmentset(object): slots = ['head', 'legs', ... ] def __init__(self, head=none, legs=none, ...) self.head = head self.legs = legs ... self.equipment = [getattr(self, item) item in self.slots] @property def x(self) return sum(item.x item in self.equipment) # similar y , z @property def val_1(self): return self.x * 2 # similar val_2, val_3 # implement dunder rich comparison methods? result = max(equipmentset(*gearset) \ gearset in itertools.permutations(all_your_gear))
Comments
Post a Comment