Cheerful Curmudgeon

A complete lack of ideas and the power to express them.

  • Home
  • About Me
    • Art Zemon’s PGP Key
    • Privacy Policy
  • Bede BD-4C
    • Hall of Fame
  • Piper Arrow

Read-Only Python Properties

September 30, 2013 Art Zemon

Following onto yesterday’s post about Python Properties and @property, there is a neat little trick to create a read-only property of a Python class. All you have to do is omit the setter. This Hero class has a read-write property “name”

class Hero(object):
    def __init__(self, **kwargs):
        self.name = kwargs['name']

    @property
    def name(self):
        return '%s %s' % (self.firstname, self.lastname)

    @name.setter
    def name(self, name):
        self.firstname, self.lastname = name.split(' ', 2)

if '__main__' == __name__:
    superman = Hero(name='Kal El')
    print superman.name
    print superman.firstname
    superman.name = 'Clark Kent'
    print superman.name
    print superman.firstname

# Kal El
# Kal
# Clark Kent
# Clark

You can see it used in the the constructor and in the “superman.name=” statement at the bottom.

We can easily make the name attribute read-only like this:

class Hero(object):
    def __init__(self, **kwargs):
        self.firstname, self.lastname = kwargs['name'].split(' ', 2)

    @property
    def name(self):
        return '%s %s' % (self.firstname, self.lastname)

if '__main__' == __name__:
    superman = Hero(name='Kal El')
    print superman.name
    print superman.firstname
    superman.name = 'Clark Kent'

# Kal El
# Kal
# Traceback (most recent call last):
#   File "/home/art/PycharmProjects/hello/Hero.py", line 14, in <module>
#     superman.name = 'Clark Kent'
# AttributeError: can't set attribute

Note that I had to change the constructor, directly setting self.firstname and self.lastname, since it is no longer valid to write “self.name =”.

Software

Recent Posts

  • Stretching a Photo April 21, 2025
  • There are Elephants in the Room April 10, 2025
  • Let’s Eliminate Real WFA April 1, 2025
  • Thumb Wrist Neck Waist Height March 18, 2025
  • Avoid Targeted Advertisements February 5, 2025

About Art Zemon

Omni-curious geek. Husband. Father. Airplane builder & pilot. Bicyclist. Photographer. Computer engineer.

Categories

  • Aviation (261)
    • Bede BD-4C (174)
    • Soaring (5)
  • Bicycling (37)
    • St. Louis to Atlanta (8)
    • St. Peters to Minneapolis (18)
  • Business (48)
  • Cabbages & Kings (24)
  • Communicating (37)
  • Ecology (21)
  • Economy (8)
  • Family (35)
  • Finding the Good (43)
  • Fun (188)
    • Six Word Stories (8)
  • Gardening (5)
  • Genealogy (5)
  • Government (35)
  • Health (67)
  • Judaism (10)
  • Men (12)
  • Mideast (5)
  • Movies (8)
  • Philosophy (15)
  • Photography (27)
  • Rants & Raves (103)
  • Recommendations (35)
  • Safety (37)
  • Science (22)
    • Biology (7)
    • Physics (7)
    • Pyschology (3)
  • Technology (195)
    • eBooks (7)
    • Internet (66)
    • Software (63)
    • VOIP (5)
  • Travel (43)
  • Tzedakah (12)
  • Women (5)

You Will Also Like

  • Art Zemon's Genealogy
  • Art Zemon's Photos
  • Mastodon @babka.social
  • Mastodon @raphus.social

Search

#DonorForLife

6 gallon blood donor badge
#DonorForLife - Give Blood - Save Lives

Archives

Copyright © 2025 · Daily Dish Pro Theme on Genesis Framework · WordPress · Log in