The Price is Right • From Properties to Descriptors

You have a class called Product that has an attribute called .selling_price. You also have an instance of Product assigned to the variable name coffee.What happens when you write coffee.selling_pri...

By · · 1 min read
The Price is Right • From Properties to Descriptors

Source: www.thepythoncodingstack.com

You have a class called Product that has an attribute called .selling_price. You also have an instance of Product assigned to the variable name coffee.What happens when you write coffee.selling_price?Sure, I know you know. The code returns the value of the .selling_price attribute. But what really happens behind the scenes?Python operations rely on special methods. The dot you use to access attributes is no exception. But strange and weird things can happen when you use the dot.Normally, the Product class determines what happens when you write coffee.selling_price. It checks whether the instance has a .selling_price attribute, typically by looking in its .__dict__ dictionary of attributes. If the instance doesn’t have this attribute, Python checks whether it’s a class attribute.But sometimes the class doesn’t get to decide what happens when you write coffee.selling_price. The object in .selling_price could take control of the attribute lookup.Let’s explore the w