4 comments on “Read-Only Properties in Python

  1. Hello, from example show in article “Set Once Property” does not come into effect:

    a = SOPTest(‘value1’)
    print a.prop
    value1
    a.prop = “value2”

    “prop” property still can be changed. Could you correct me if I take a misunderstand ?

    Like

  2. For SetOnceProperty since val is a positional argument it will always need to be set when we create an instance. How will this work if we want to set it to something different? Could you give a usage example where we initialize and then set the value just once.?

    Thanks

    Like

    • Assuming I understand your question properly, you can set it up to optionally set it later like this:

      class SOPTest():
      prop = SetOnceProperty()

      def __init__(self, val=None):
          if val is not None:
              self.prop = val
      

      Or you can force doing it later like this:

      class SOPTest():
      prop = SetOnceProperty()

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s