0

How can I protect in Python class methods from beeing mistakenly changed? Is there some kind of a "write protection"?

Example:

class bar():

    def blob(self):
        return 2

if __name__ == "__main__":

    foo = bar()
    print(foo.blob()) # Returns 2

    foo.blob = 1 # Overwrites the method "blob" without a warning!
                 # foo.blob returns 1, foo.blob() is not callabele anymore :( 

    foo.blib = 1 # Is also possible

    print(foo.blob)
    print(foo.blob())

When I call this script returns:

2
1
Traceback (most recent call last):
  File "blob.py", line 18, in <module>
    print(foo.blob())
TypeError: 'int' object is not callable

I would prefer do get a warning.

oholimoli
  • 73
  • 1
  • 9

0 Answers0