Guest User

Python property decorator example

a guest
May 29th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.33 KB | None | 0 0
  1. class Test:
  2.     def __init__(self, my_val):
  3.         self._myval = my_val
  4.  
  5.     @property
  6.     def my_val(self):
  7.         return "Value: {0}".format(self._myval)
  8.  
  9.     @my_val.setter
  10.     def my_val(self, value):
  11.         raise RuntimeError("My val property is read only")
  12.  
  13.  
  14. inst = Test("abc")
  15. print(inst.my_val)
  16. inst.my_val = "xyz"
Advertisement
Add Comment
Please, Sign In to add comment