Created
February 13, 2015 17:17
-
-
Save dmiro/8a38c3af11654d24141b to your computer and use it in GitHub Desktop.
Para tener un read-only @Property decorator DEBES heredar del nuevo estilo de clases "object"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| To have a 'read-only' property in a class you can make use of the @property decoration, you'll need to inherit from object when you do so to make use of the new-style classes. | |
| Example: | |
| >>> class A(object): | |
| ... def __init__(self, a): | |
| ... self.__a = a | |
| ... @property | |
| ... def a(self): | |
| ... return self.__a | |
| ... | |
| >>> a = A('test') | |
| >>> a.a | |
| 'test' | |
| >>> a.a = 'pleh' | |
| Traceback (most recent call last): | |
| File "<stdin>", line 1, in <module> | |
| AttributeError: can't set attribute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment