Is it possible to delete a method from an object (not class) in python?

前端 未结 8 1771
借酒劲吻你
借酒劲吻你 2020-12-31 13:24

I have a class with a few methods, some of which are only valid when the object is in a particular state. I would like to have the methods simply not be bound to the objects

8条回答
  •  情书的邮戳
    2020-12-31 13:56

    You can't delete a class method from an instance of that class because the instance doesn't have that method. The protocol is: if o is an instance of class Foo, and I call o.bar(), first o is examined to see if it has a method named bar. If it doesn't, then Foo is examined. The methods aren't bound to the instance unless they override its class.

    I don't see any good that can come from the road that you're going down here, either.

提交回复
热议问题