Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Square:
def __init__(self, side):
self.side = side
@property
def area(self):
return self.side * self.side
square = Square(3)
print(square.area)Top-level scope
class SquareObject 3 · SquareSquare({'side': 3})property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.
(no output)
What do you expect this program to print?
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})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Square:
def __init__(self, side):
self.side = side
@property
def area(self):
return self.side * self.side
square = Square(3)
print(square.area)Top-level scope
class SquareObject 3 · SquareSquare({'side': 3})The example has finished. Compare the final output with the values and references above.
9
What do you expect this program to print?
class Square:
def __init__(self, side):
self.side = side
@property
def area(self):
return self.side * self.side
square = Square(3)
print(square.area)property() FunctionThe 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.
property(fget=None, fset=None, fdel=None, doc=None)
fget – Function to get the value (getter)fset – Function to set the value (setter)fdel – Function to delete the value (deleter)doc – Optional docstringclass 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)
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...
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...
_name) for internal variablesproperty() without defining fget – it won’t be readable__init__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.
property() lets you control access to attributes in classes.fget, fset, fdel to manage how values are accessed, set, or deleted.@property decorator is a modern and cleaner alternative.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
Connect what you noticed in the example with the idea below.
property exposes managed attribute access. A getter runs when the attribute is read, without requiring method-call syntax.
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.