I have a problem in assigning values to objects

Here is the link of the lesson: https://gist.github.com/2772f4da7de29d0e0b7282afcaae42da
The second point of the lesson instructed me to Use Song ‘s built-in .add_title() method to add a title to electric_relaxation : "Electric Relaxation" .
In the theory of the lesson it is said:
We can give the object’s attributes values like this (note that these must be attributes you defined in the class):

accra.population = 2270000;

So I wrote

 electric_relaxation.title = "Electric Relaxation";

My program was incorrect (it gave a cross mark at the second point).
In the solution

 'electric_relaxation.add_title("Electric Relaxation");' was given.

Where am I wrong?
Edit: I did not format the question properly.

1 Like

The instruction specifically said to use the .add_title() method, which is a special type of method that object-oriented programming (OOP) enthusiasts call a “setter”, because it sets an object attribute.

What you did was to directly set the attribute, using the object name and dot notation. You can do that in Python (but not in some other languages), but it is not recommended, and the aforementioned OOP enthusiasts insist upon always using a setter (and it’s counterpart, a “getter”.)

Why is it not recommended? I think that it is easier to directly set the attribute than declaring another variable and doing it indirectly.

1 Like

If that’s all an object is, then I agree with you, and get pretty annoyed when such code has getters/setters.

You can think of methods as message handlers. The object can act however it pleases to the messages it gets, as opposed to having its memory accessed directly. The object is then free to do integrity checks on actions suggested to it, and perhaps the context of the object is such that it needs to react to changes.

Sorry, I couldn’t understand what you said in the second paragraph(You can think of methods as message handlers. …).
Could you illustrate the point you were trying to explain to me?

Many people agree with you.

I’d bet that the people who composed the Codecademy Python courses - at least those parts dealing with OOP - come from a Java background. You could take a dozen Python courses and never see the words getter or setter.