python - Changing dynamically one method of a class -


my question twofold. first, define class having 1 method scale behaves differently depending on value assigned 1 of properties. below current attempt xxx signling parts of code missing.

class c(object):     def __init__(self):         self._scaling_method = none      def same(self, u):         return u      def double(self, u):         return u * 2      scale = same      @property     def scaling_method(self):         return self._scaling_method      @scaling_method.setter     def scaling_method(self, value):         if value.lower() == "double":             self._scaling_method = "double"             print "scaling method set 'double'."             # xxx method scale should set double         elif value.lower() == "same":             self._scaling_method = "same"             print "scaling method set 'same'."             # xxx method scale should set same         else:             print "unknown scaling method." 

if c instance of c,i want able

c.same(3) # yields 3 c.double(3) # yields 6 c.scale(3) # yields 3 because scale defaults same c.scaling_method = "double" c.scale(3) # yields 6 

once correctly set up, able set scaling_method property argument , define scale method accordingly in __init__. now, solution see second part define each scaling method both in core of class , in __init__ section, seems pretty awkward.

edit: noticed this answer may adapted here.

as commented, not clear if want on class basis or instance basis. here solution work on instance basis. overload scale method:

class c(object):     def __init__(self):         self._scaling_method = none      def same(self, u):         return u      def double(self, u):         return u * 2      scale = same      @property     def scaling_method(self):         return self._scaling_method      @scaling_method.setter     def scaling_method(self, value):         if value.lower() == "double":             self._scaling_method = "double"             self.scale = self.double         elif value.lower() == "same":             self._scaling_method = "same"             self.scale = self.same         else:             print "unknown scaling method." 

if want on class basis, replace self.scale = ... self.__class__.scale = .... note: considered hacky because i'm changing class.

then

sage: c = c() sage: c.scale(3) 3 sage: c.scaling_method = "double" sage: c.scale(3) 6 

note other objects of class not affected:

sage: cc = c() sage: cc.scale(4) 8 

if want have same behavior on class basis:

class c(object):     _scaling_method = none      def same(self, u):         return u      def double(self, u):         return u * 2      scale = same      @property     def scaling_method(self):         return self._scaling_method      @scaling_method.setter     def scaling_method(self, value):         if value.lower() == "double":             self.__class__._scaling_method = "double"             self.__class__.scale = self.double         elif value.lower() == "same":             self.__class___.scaling_method = "same"             self.__class__.scale = self.same         else:             print "unknown scaling method." 

then in previous code:

sage: c = c(); cc = c() sage: c.scale(2), cc.scale(3) (2, 3) sage: c.scaling_method = "double" sage: c.scale(2) 4 

but cc affected too:

sage: cc.scale(3) 6 

Comments