Hide a bank balance with __name and @property, after seeing the bug it prevents.
In one line
Keep an object's data behind underscore names and let only methods with checks change it. When outside code only needs to read a value, use @property.
Why you need it
If any line can run acc.balance -= 3500, as in the buggy code below, money leaves even when the balance is too low. Adding checks does not help much when balance-changing lines are scattered everywhere and one of them is forgotten.
Hide the balance as __balance and change it only in deposit and withdraw, and the checks and the log live in one place. A new withdrawal rule means editing one method.
Everyday picture
Think of a self-checkout machine. Shoppers never open the cash box; they tap Pay on the screen, and the machine checks the amount. The cash box is the hidden value, the screen buttons are the methods.
Press Run to try your edited code right away. The step-through and the output above follow the original code.
When to use it
Attributes that must never hold a wrong value (balance, stock, score, age)
Changes that must trigger something else (a log entry, a notice, a recalculation)
Lists or dicts you show to outside code. Return a copy.
Python has no keyword that locks a value the way private does in Java. _name means "internal, please do not touch", and __name renames the attribute so subclasses do not overwrite it by accident.
The read-only error text "has no setter" appears from Python 3.11. Older versions say "can't set attribute".
Common mistakes
Believing __name can never be changed
__balance is only renamed to _Account__balance; it is not locked. Anyone who knows the new name can still write it. Hiding in Python is an agreement, and using the methods you provided is what keeps the data safe.
Giving the property and the stored value the same name
self.balance = start tries to write the property balance, which has no setter. Store the value under another name such as _balance and let the property read it.
Answer: 1. Trueself.__balance, written inside the class, is stored as _Account__balance. So yes, the key is there.
Questions people ask
Does Python have private?
There is no keyword. _name is an agreement that it is internal, and __name is renamed to _Class__name, which still does not lock it from outside.
When should I use @property?
When outside code should only read a value, or when reading or writing needs a check or a calculation. Callers read it like an attribute, without parentheses.
Should I write getters and setters for everything?
No. Python code usually starts with plain attributes and switches to @property when a check is needed. Calling code does not change.