Function to reference class variable in Python -
i have program uses python windows extensions control mouse. i'm trying make function calls class variable (i think that's they're called).
anyways code looks this:
class mouse: def move_mouse(self, pos): """move mouse specified coordinates""" (x, y) = pos old_pos = self.get_position() x = x if (x != -1) else old_pos[0] y = y if (y != -1) else old_pos[1] self._do_event(self.mouseeventf_move + self.mouseeventf_absolute, x, y, 0, 0) it's called this:
mouse = mouse(); position = (3,5); #some coordinate (where mouse on screen) mouse.move_mouse(position); i'm wondering if can create function make easier call move_mouse() function. can call position have defined? can create work this:
positions = {"0": (123,432), "1": (312,123)} def move(x); mouse.move(positions[str(x)]); >>>move(0) and function should proceed move first entry in positions dictionary.
i can't stop getting error when run it. know why doesn't work?
one approach class attribute of pre-defined positions, when mouse.move_mouse called either tries retrieve pos dictionary, assuming it's key, or uses directly if isn't key:
class mouse: positions = {'home': (0, 0), ...} def move_mouse(self, pos): """move mouse specified coordinates""" x, y = self.positions.get(pos, pos) ... now both mouse.move_mouse('home') , mouse.move_mouse((0, 0)) have same effect.
Comments
Post a Comment