Properties

Property Decorator

  1. # @property for getter
  2. class Circle:
  3. def __init__(self, radius):
  4. self._radius = radius
  5. @property
  6. def radius(self):
  7. return self._radius
  8. Circle(5).radius
  9. #ans: 5 (accessed like attribute)

Property Setter

  1. # @property.setter
  2. class Circle:
  3. def __init__(self, radius):
  4. self._radius = radius
  5. @property
  6. def radius(self):
  7. return self._radius
  8. @radius.setter
  9. def radius(self, value):
  10. if value < 0:
  11. raise ValueError("Negative")
  12. self._radius = value

Computed Property

  1. # property with calculation
  2. class Circle:
  3. def __init__(self, radius):
  4. self._radius = radius
  5. @property
  6. def area(self):
  7. return 3.14 * self._radius ** 2
  8. Circle(5).area
  9. #ans: 78.5

Read-Only Property

  1. # property without setter
  2. class Person:
  3. def __init__(self, name):
  4. self._name = name
  5. @property
  6. def name(self):
  7. return self._name
  8. person = Person("Alice")
  9. #ans: person.name = "Bob" # AttributeError

Property Deleter

  1. # @property.deleter
  2. class Person:
  3. def __init__(self, name):
  4. self._name = name
  5. @property
  6. def name(self):
  7. return self._name
  8. @name.deleter
  9. def name(self):
  10. del self._name
  11. person = Person("Alice")
  12. del person.name

Exercises - Part 1

  1. # property vs method?
  2. class MyClass:
  3. @property
  4. def value(self):
  5. return 5
  6. obj = MyClass()
  7. obj.value
  8. #ans: 5 (no parentheses)

Exercises - Part 2

  1. # setter without getter?
  2. class MyClass:
  3. @value.setter
  4. def value(self, x):
  5. pass
  6. #ans: NameError (@property needed first)

Exercises - Part 3

  1. # property assignment?
  2. class MyClass:
  3. @property
  4. def value(self):
  5. return 5
  6. obj = MyClass()
  7. obj.value = 10
  8. #ans: AttributeError (no setter)

Exercises - Part 4

  1. # property caching?
  2. class MyClass:
  3. @property
  4. def value(self):
  5. print("Computing")
  6. return 5
  7. obj = MyClass()
  8. obj.value
  9. obj.value
  10. #ans: prints "Computing" twice

Exercises - Part 5

  1. # property validation?
  2. class MyClass:
  3. @property
  4. def x(self):
  5. return self._x
  6. @x.setter
  7. def x(self, value):
  8. if value < 0:
  9. raise ValueError()
  10. self._x = value

Exercises - Part 6

  1. # property in __init__?
  2. class MyClass:
  3. def __init__(self, x):
  4. self.x = x # calls setter
  5. @property
  6. def x(self):
  7. return self._x
  8. @x.setter
  9. def x(self, value):
  10. self._x = value

Exercises - Part 7

  1. # delete without deleter?
  2. class MyClass:
  3. @property
  4. def value(self):
  5. return 5
  6. obj = MyClass()
  7. del obj.value
  8. #ans: AttributeError

Exercises - Part 8

  1. # property inheritance?
  2. class Base:
  3. @property
  4. def x(self):
  5. return 5
  6. class Derived(Base):
  7. pass
  8. Derived().x
  9. #ans: 5

Exercises - Part 9

  1. # override property?
  2. class Base:
  3. @property
  4. def x(self):
  5. return 5
  6. class Derived(Base):
  7. @property
  8. def x(self):
  9. return 10
  10. Derived().x
  11. #ans: 10

Exercises - Part 10

  1. # property with args?
  2. class MyClass:
  3. @property
  4. def value(self, arg):
  5. return arg
  6. #ans: TypeError (property takes no args)

Google tag (gtag.js)