Site icon DataFlair

Python Property – The Problem and Solution

Python Property

Python Property

Python course with 57 real-time projects - Learn Python

In this tutorial on Python Property, you will learn how to use setters and getters using property.

Moreover, we will discuss the problems and solutions related to Python Property. Before we begin, take a look at Classes and Objects.

So, let’s start Python Property Tutorial.

Python Property

What is Python Property?

Let’s first look at a Python class Song.

>>> class Song:
	def __init__(self,title):
		self.title=title
	def show_title(self):
		print(f"I'm listening to {self.title}")

We then create an object Teddy_Bear, and call show_title() on it.

>>> Teddy_Bear=Song('Teddy Bear')
>>> Teddy_Bear.show_title()

Output

I’m listening to Teddy Bear

Here, we can get and set the title instance attribute this way:

>>> Teddy_Bear.title #getting the title

Output

‘Teddy Bear’
>>> Teddy_Bear.title='TEDDY BEAR' #setting the title
>>> Teddy_Bear.title

Output

‘TEDDY BEAR’

Checking the object’s dictionary, we find this:

>>> Teddy_Bear.__dict__

Output

{‘title’: ‘TEDDY BEAR’}

The problem: What if clients begin accessing and modifying the variable ‘title’? That could cause all kinds of chaos.

Python Property – A Possible Solution

Let’s do two things:

>>> def get_title(self):
	return self._title
>>> def set_title(self,title):
		self._title=title.upper()

Here, get_title returns the title of the book, and set_title converts it to all caps (we saw this in

Python Built-in Functions)

>>> class Song:
	def __init__(self,title):
		self.title=title
	def show_title(self):
		print(f"I'm listening to {self.title}")
	def get_title(self):
		return self._title
	def set_title(self,title):
		self._title=title.upper()

Well, the underscore doesn’t really make the variable private, because Python does not implement any such restriction at its level.

But these are the norms that we must follow.

But the problem with this solution is that we will need to update ‘Teddy_Bear.title’ to ‘Teddy_Bear.get_title()’ and ‘Teddy_Bear.title=’TEDDY BEAR’’ to ‘Teddy_Bear.set_title()’ in each occurrence.

Real-world code, clients may have thousands of lines where they implemented our class.

In that case, it becomes tedious to refactor every single occurrence. This makes it not backward-compatible.

So, let’s look at a better option next. Actually, we will just add a line to this approach, and it will work like magic.

Python Property – The Solution

To solve this problem, Python lends us the function property(). This is what this property of Python looks like:

property(fget, fset, fdel, doc)

Here, fget takes a getter, fset takes a setter, fdel takes a function to delete an attribute, and doc is a string.

These arguments, however, are optional. Here is a call to property() without any arguments:

>>> property()

Output

<property object at 0x062CDDB0>

For fget, fset, and fdel, a property object has the following methods: getter(), setter(), and delete(). So, this is equivalent to this:

>>> title=property() #make empty property
>>> title=title.getter(get_title) #assign fget
>>> title=title.setter(set_title) #assign fset

For our class Song, we add the following line:

>>> title=property(get_title,set_title)

This makes the class look like this:

>>> class Song:
  def __init__(self,title):
    self.title=title
  def show_title(self):
    print(f"I'm listening to {self.title}")
  def get_title(self):
    return self._title
  def set_title(self,title):
    self._title=title.upper()
  title=property(get_title,set_title)

Now, let’s try accessing the title for the object Teddy_Bear.

>>> Teddy_Bear=Song('Teddy Bear')
>>> Teddy_Bear.title

Output

‘TEDDY BEAR’
>>> Teddy_Bear.show_title()

Output

I’m listening to TEDDY BEAR

This implementation is backward-compatible. This saves us from all the work of refactoring.

Also note that it is _title that holds the actual value of the book’s title. ‘title’ is just a property object that makes it happen.

Some Syntactic Sugar

We have seen the @-syntax in our lesson on Python Property Decorators. Here, we do this:

>>> class Song:
	def __init__(self,title):
		self.title=title
	def show_title(self):
		print(f"I'm listening to {self.title}")
	@property
	def title(self):
		return self._title
	@title.setter
	def title(self,title):
		self._title=title.upper()

Firstly, note that we removed the call to property. Next, we ditched the names ‘get_title’ and ‘set_title’, and used ‘title’ instead.

Finally, we used @property before the getter, and @title.setter before the setter.

Let’s access this now.

>>> Teddy_Bear=Song('Teddy Bear')
>>> Teddy_Bear.title

Output

‘TEDDY BEAR’
>>> Teddy_Bear.show_title()

Output

I’m listening to TEDDY BEAR

So, this was all about Python Property Tutorial. Hope you like our explanation.

Python Interview Questions on Property

  1. What is property in Python?
  2. What is the use of property in Python?
  3. What is Python class property?
  4. How does Python property work?
  5. What is the benefit of using property in Python?

Conclusion

In this tutorial on Python Property, we learned about different problem and solutions of this problems.

Exit mobile version