Dynamically adding methods to a class
python
Solution
Because you use the same scope for all function declarations. Define each function in a separate scope.
Problem
I'm trying to add methods to a class based on a list. ``` class _Roles(object): """ set the roles for dev, staging and production """ def __init__(self): from types import MethodType steps = ['dev','stage','prod'] for step in steps: def env_setter(self): print step method = MethodType(env_setter,self,self.__class__) setattr(self,step,method) ``` The problem is that when I call `_Roles.dev()`, `_Roles.stage()`, or `_Roles.prod()`, I always get printed the last step that is prod instead of getting dev for `dev()` and so on. what's the reason for this?