1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Object-Oriented Programming in Python

Connected

Exercise

Adding methods and attributes

Now you'll start by creating methods that set attributes, and then add a few methods that manipulate them.

As mentioned in the first video, an object-oriented approach is most useful when your code involves complex interactions of many objects. In real production code, classes can have dozens of attributes and methods with complicated logic, but the underlying structure is the same as with the most simple class.

Your classes in this course will only have a few attributes and short methods, but the organizational principles behind them will be directly translatable to more complicated code.

Instructions

100 XP
  • Add another method to the Employee class called set_salary() that will set the salary attribute of an object to the new_salary argument passed to the method.
  • Call the emp object's .set_name() method, assigning the value 'Korel Rossi'.
  • Call the method on the emp object and set the salary to 50000.
  • Print the salary attribute of the emp object.