BankAccount with a private attribute _balance.@property method called balance to get the current balance.balance that only allows setting a non-negative value.Jump into concepts and practice - no test required
BankAccount with a private attribute _balance.@property method called balance to get the current balance.balance that only allows setting a non-negative value.BankAccount with an __init__ method that sets a private attribute _balance to 0.Use self._balance = 0 inside the __init__ method to create a private balance.
@property method called balance inside the BankAccount class that returns self._balance.Use @property above the balance method to make it a getter.
balance using @balance.setter that sets self._balance only if the new value is not negative. If the value is negative, do not change self._balance.Use @balance.setter to create the setter method. Check if value is not negative before setting.
BankAccount called account. Set account.balance to 100. Then print account.balance.Create the object, set the balance to 100, then print the balance to see the result.
What does the @property decorator do in a Python class?
@property@property decorator lets you call a method without parentheses, like an attribute.Which of the following is the correct syntax to define a setter for a property named value?
class MyClass:
@property
def value(self):
return self._value
# What goes here?@value.setter and defines a method with the same name value.@value.setter and method value. Others use wrong decorator or method names.What will be the output of the following code?
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value < 0:
self._radius = 0
else:
self._radius = value
c = Circle(5)
c.radius = -3
print(c.radius)radius, if value < 0, it sets _radius to 0, else to value.c.radius = -3 triggers setter, sets _radius to 0. Printing c.radius returns 0.Find the error in this code using property decorators and fix it.
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def set_name(self, value):
self._name = value
p = Person('Alice')
p.name = 'Bob'
print(p.name)@name.setter requires the method to be named name, but here it is set_name.name to match the property name and decorator.name instead of set_name. -> Option AConsider a class that stores a temperature in Celsius internally but exposes it as Fahrenheit using property decorators. Which code correctly implements this?
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def fahrenheit(self):
# Convert Celsius to Fahrenheit
return (self._celsius * 9/5) + 32
@fahrenheit.setter
def fahrenheit(self, value):
# Convert Fahrenheit to Celsius
self._celsius = (value - 32) * 5/9
# Usage
temp = Temperature(0)
temp.fahrenheit = 212
print(round(temp._celsius))What is the output?
temp.fahrenheit = 212 calls setter, converts 212°F to Celsius: (212-32)*5/9 = 100. Printing temp._celsius rounded gives 100.