Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if property is settable/deletable

How do I check if a property is settable or deletable in Python?

The best I've found so far is

type(obj).__dict__["prop_name"].fset is not None
like image 202
Neil G Avatar asked May 23 '11 20:05

Neil G


1 Answers

This is a good case when you should subscribe to "It's Easier to Ask for Forgiveness than Permission" philosophy, and just handle the exception in case property is not settable/deletable.

try:
    x.prop = 42
except AttributeError:
    pass
like image 78
Cat Plus Plus Avatar answered Oct 18 '22 18:10

Cat Plus Plus