Python Reference Library →

Python property() Function – Create Managed Attributes

Let’s look at property() function – create managed attributes

  1. Notice

    property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Square:
    def __init__(self, side):
        self.side = side
    @property
    def area(self):
        return self.side * self.side
square = Square(3)
print(square.area)
Live state

Top-level scope

Square→Object 2
square→Object 3
Object 2 · typeclass SquareObject 3 · SquareSquare({'side': 3})
This step

property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.

Picture property() function – create managed attributes

property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.

Image brief

Alt text
A diagram for Python property() Function – Create Managed Attributes. Instructions: class Square: def __init__(self, side): self.side = side @property def area(self): return self.side * self.side square = Square(3) print(square.area). Displayed result: 9. The final example bindings are Square --> object 2 (type); square --> object 3 (Square). object 2: class Square; object 3: Square({'side': 3})
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python property() Function – Create Managed Attributes". Teaching focus: property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS class Square: def __init__(self, side): self.side = side @property def area(self): return self.side * self.side square = Square(3) print(square.area) | v DISPLAYED RESULT 9 FINAL BINDINGS Square --> object 2 (type) square --> object 3 (Square) object 2: class Square object 3: Square({'side': 3}) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python property() Function – Create Managed Attributes. Instructions: class Square: def __init__(self, side): self.side = side @property def area(self): return self.side * self.side square = Square(3) print(square.area). Displayed result: 9. The final example bindings are Square --> object 2 (type); square --> object 3 (Square). object 2: class Square; object 3: Square({'side': 3})

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Square:
    def __init__(self, side):
        self.side = side
    @property
    def area(self):
        return self.side * self.side
square = Square(3)
print(square.area)
Live state

Top-level scope

Square→Object 2
square→Object 3
Object 2 · typeclass SquareObject 3 · SquareSquare({'side': 3})
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
9

What do you expect this program to print?

What does this example display?

class Square:
    def __init__(self, side):
        self.side = side
    @property
    def area(self):
        return self.side * self.side
square = Square(3)
print(square.area)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

9

Follow the printed values in order. The output is: 9

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.

property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.

More examples and reference

Python property() Function

The property() function in Python is used to create managed attributes in object-oriented programming. It lets you define methods that act like attributes – helpful for adding logic when accessing or updating a value.

Syntax

property(fget=None, fset=None, fdel=None, doc=None)

Parameters:

  • fget – Function to get the value (getter)
  • fset – Function to set the value (setter)
  • fdel – Function to delete the value (deleter)
  • doc – Optional docstring

Returns:

  • A property object that can be assigned to a class attribute

Example: Basic Usage of property()

class Person:
    def __init__(self, name):
        self._name = name

    def get_name(self):
        print("Getting name...")
        return self._name

    def set_name(self, value):
        print("Setting name...")
        self._name = value

    def del_name(self):
        print("Deleting name...")
        del self._name

    name = property(get_name, set_name, del_name)

Usage:

p = Person("Alice")
print(p.name)      # Getting name... → Alice
p.name = "Bob"     # Setting name...
print(p.name)      # Getting name... → Bob
del p.name         # Deleting name...

Modern Alternative: Using @property Decorator

class Person:
    def __init__(self, name):
        self._name = name

    @property
    def name(self):
        print("Getting name...")
        return self._name

    @name.setter
    def name(self, value):
        print("Setting name...")
        self._name = value

    @name.deleter
    def name(self):
        print("Deleting name...")
        del self._name
Getting name...
Setting name...
Deleting name...

Why Use property()?

  • Encapsulation: Control how attributes are accessed or modified
  • Cleaner syntax: Looks like direct attribute access
  • Backward compatibility: Add logic without changing the API

Common Mistakes

  • Not using the underscore convention (e.g., _name) for internal variables
  • Using property() without defining fget – it won’t be readable
  • Using property incorrectly inside __init__

Interview Tip

Interviewers often ask about encapsulation and data hiding. Showing that you know how to use property and @property in Python can demonstrate your grasp of object-oriented principles.

Summary

  • property() lets you control access to attributes in classes.
  • You can use fget, fset, fdel to manage how values are accessed, set, or deleted.
  • The @property decorator is a modern and cleaner alternative.

Practice Problem

Create a class Temperature with a Celsius attribute. Use @property to add a computed Fahrenheit property (read-only).

class Temperature:
    def __init__(self, celsius):
        self._celsius = celsius

    @property
    def fahrenheit(self):
        return (self._celsius * 9/5) + 32

t = Temperature(25)
print(t.fahrenheit)  # Output: 77.0

What to remember

Connect what you noticed in the example with the idea below.

Remember

property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.

Watch out for this idea

A property getter is accessed only by adding parentheses.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →