Making functions non override-able

后端 未结 5 1034
我寻月下人不归
我寻月下人不归 2020-12-05 03:05

I know python functions are virtual by default. Let\'s say I have this:

class Foo:
    def __init__(self, args):
        do some stuff
    def goo():
                


        
5条回答
  •  盖世英雄少女心
    2020-12-05 03:31

    Since Python has monkey patching, not only can you not make anything "private". Even if you could, someone could still monkeypatch in a new version of the method function.

    You can use this kind of name as a "don't go near" warning.

    class Foo( object ):
        def _roo( self ):
           """Change this at your own risk."""
    

    That's the usual approach. Everyone can read your source. They were warned. If they boldly go where they were warned not to go, they get what they deserve. It doesn't work and you can't help them.

    You can try to make this intentionally obcure with inner classes and "hidden" implementation modules that are called by the "private" methods. But... everyone has your source. You can't prevent anything. You can only advise people of the consequences of their actions.

提交回复
热议问题