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

前端 未结 8 1749
借酒劲吻你
借酒劲吻你 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 14:14

    While I agree that this is the wrong solution, my way to "remove" a method would be to overwrite the function with a property that raises an AttributeError. This way hasattr gives the correct result, but it is not as much hassle as using dir.

提交回复
热议问题