Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Properties | Encapsulation
In-Depth Python OOP

PropertiesProperties

Properties are a mechanism in object-oriented programming that allow controlled access to protected and private attributes. They are defined using methods and can be accessed as if they were regular attributes. Properties provide a convenient and intuitive way to interact with an object's internal data.

Please take a look at the example below to get a general idea of how properties work, without diving too deep into the details:

Code Description
In the provided code, the Person class demonstrates the usage of properties. The class has a constructor method __init__ that takes a parameter name and initializes the _name attribute with the provided value.

The name attribute is encapsulated within a property using the @property decorator. This decorator indicates that the method directly below it (name method in this case) should be treated as a getter method for the name property. The getter method, in this case, simply returns the value of the _name attribute.

To allow modification of the name attribute, a setter method is defined using the @name.setter decorator. This setter method, also named name, takes a value parameter. When the property is assigned a new value (e.g., person.name = "Bob"), this setter method is called with the assigned value. In this case, the setter method sets the value of the _name attribute to the provided value.

The usage of the properties is demonstrated by creating an instance of the Person class named person with the name "John". The property name is accessed using person.name, which invokes the getter method and returns the value of the _name attribute ("John"). The property is then modified by assigning a new value to it (person.name = "Bob"), which calls the setter method and updates the _name attribute to "Bob". Finally, the modified property is accessed again using person.name, resulting in the value "Bob" being printed.

In summary, properties in Python provide a way to encapsulate attribute access and modification within getter and setter methods. They allow you to define custom behavior when accessing or modifying an attribute, giving you more control over the attribute's behavior while providing a clean and consistent interface to interact with the class attributes.

Please note that this example is meant to provide a basic understanding of properties and their usage. More advanced concepts and nuances will be covered in subsequent chapters.

Everything was clear?

Section 3. Chapter 6
course content

Course Content

In-Depth Python OOP

PropertiesProperties

Properties are a mechanism in object-oriented programming that allow controlled access to protected and private attributes. They are defined using methods and can be accessed as if they were regular attributes. Properties provide a convenient and intuitive way to interact with an object's internal data.

Please take a look at the example below to get a general idea of how properties work, without diving too deep into the details:

Code Description
In the provided code, the Person class demonstrates the usage of properties. The class has a constructor method __init__ that takes a parameter name and initializes the _name attribute with the provided value.

The name attribute is encapsulated within a property using the @property decorator. This decorator indicates that the method directly below it (name method in this case) should be treated as a getter method for the name property. The getter method, in this case, simply returns the value of the _name attribute.

To allow modification of the name attribute, a setter method is defined using the @name.setter decorator. This setter method, also named name, takes a value parameter. When the property is assigned a new value (e.g., person.name = "Bob"), this setter method is called with the assigned value. In this case, the setter method sets the value of the _name attribute to the provided value.

The usage of the properties is demonstrated by creating an instance of the Person class named person with the name "John". The property name is accessed using person.name, which invokes the getter method and returns the value of the _name attribute ("John"). The property is then modified by assigning a new value to it (person.name = "Bob"), which calls the setter method and updates the _name attribute to "Bob". Finally, the modified property is accessed again using person.name, resulting in the value "Bob" being printed.

In summary, properties in Python provide a way to encapsulate attribute access and modification within getter and setter methods. They allow you to define custom behavior when accessing or modifying an attribute, giving you more control over the attribute's behavior while providing a clean and consistent interface to interact with the class attributes.

Please note that this example is meant to provide a basic understanding of properties and their usage. More advanced concepts and nuances will be covered in subsequent chapters.

Everything was clear?

Section 3. Chapter 6
some-alt