Using Python class properties decorators.

In python we can use class properties declaration,getter and setters to validate data or for using custom computation.

class Book(object):
@property
def title(self):
return self._title
@title.setter
def title(self, value):
if value is None or "" == value.strip():
raise ValueError("Title should have proper value!")
self._title = value
return self._title
if __name__=='__main__':
b = Book()
b.title = "Intro Of Python"
print b.title

Check Python DOCS

This property decorator only works for New Style Python classes

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.