Back to Freecodecamp

Step 5

curriculum/challenges/english/blocks/workshop-salary-tracker/68c27fa1697ceb7c578b2c9f.md

latest1.4 KB
Original Source

--description--

The @property decorator is used in Python to turn a method into a property. It is typically used to define getter methods, which are methods used to retrieve the value of an attribute:

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

    @property
    def name(self):
        return self._name

p = Person('Alice')
print(p.name)  # Alice

Create a method named name with a self parameter and decorate it with @property. Inside the method, return self._name.

--hints--

Your Employee class should have a name method with a self parameter.

js
({ test: () => assert(runPython(`_Node(_code).find_class("Employee").find_function("name").has_args("self")`)) })

Your name method should be decorated with @property.

js
({ test: () => assert(runPython(`_Node(_code).find_class("Employee").find_function("name").has_decorators("property")`)) })

Your name method should return self._name.

js
({ test: () => assert(runPython(`_Node(_code).find_class("Employee").find_function("name").has_return("self._name")`)) })

--seed--

--seed-contents--

py
--fcc-editable-region--
class Employee:
    def __init__(self, name, level):
        self._name = name
        self._level = level

    def __str__(self):
        return f'{self._name}: {self._level}'

    

charlie_brown = Employee('Charlie Brown', 'trainee')
print(charlie_brown)
--fcc-editable-region--