2017-02-26 57 views
0
class something(object): 
      def __init__(self): 
        pass 

      def method1(self): 
        pass 

      def method2(self): 
        pass 

      def method3(self): 
        pass 

      def disable_method12(self): 

我想写禁用方法1和method2在我的课,但不会禁用方法3的方法“disable_method12”一类的一些方法,我怎么能做到这一点的方法?我要写信编写禁用蟒蛇

  def disable_method12(self): 
        method1(self) = False 
        method2(self) = False 
+0

你不能禁止的东西只有'del' – abccd

回答

2

,你可以删除这样的方法:(感谢@volcano)

def disable_method12(self): 
     del something.method1 
     del something.method2 

或更好这样的,所以你可以重命名你的类,它仍然有效

def disable_method12(self): 
     del self.__class__.method1 
     del self.__class__.method2 

测试method2电话:

s = something() 
s.method2() # first time it works 
s.disable_method12() 
s.method2() 

我在最后一行得到:

AttributeError: 'something' object has no attribute 'method2' 
+0

感谢!!!!!!!!!! – joe

+0

只是好奇 - 你试过声明* disable_method *作为一个类的方法?要么 - 或* self .__ class __。method1 *。在其方法中明确命名类看起来有点错误 – volcano

+0

好评,编辑。我尝试了类和静态方法,它也可以。 –