Legends ofPythos
Claim your name
Objects and Classes

Keeping internals private-ish

Lesson 11 of 13

Watch the lesson5:51 · with Torsten
In many languages, you can mark fields as private so other code cannot touch them. Python takes a different approach: it trusts that developers will behave.

The single underscore convention

class BankAccount:
    def __init__(self, owner):
        self.owner = owner
        self._balance = 0.0

acc = BankAccount('Ada')
print(acc._balance) # prints 0.0
_balance signals 'internal use only'
Prefixing an attribute with a single underscore (like _balance) is a convention. It tells other programmers: "This is for the class's internal machinery. Don't rely on it or change it directly." Python does not actually prevent access, though.

Using @property to control reads

class Player:
    def __init__(self):
        self._health = 100

    @property
    def health(self):
        return self._health

p = Player()
print(p.health) # prints 100
# p.health = 50 would raise AttributeError
@property makes health look like an attribute but run code
The @property line, right above the def, is called a decorator: a line starting with @ that changes the method below it. This one lets you define a method that behaves like a read-only attribute. When someone writes p.health, Python calls your function and returns the result, instead of letting them poke at _health directly.

Adding validation with setters

class Player:
    def __init__(self):
        self._level = 1

    @property
    def level(self):
        return self._level

    @level.setter
    def level(self, value):
        whole = isinstance(value, int)
        if not whole or value < 1:
            raise ValueError("invalid level")
        self._level = value

p = Player()
p.level = 5
print(p.level)   # 5
@name.setter lets you validate before storing
By attaching a setter to the same property name, you can check incoming values. If they are invalid, you raise an exception and never store bad data in _level: here, p.level = -2 stops with ValueError: invalid level. This keeps your object's state consistent.

Your turn

0 of 2 solved

Exercise 1

+35 XP
Create a class called Thermometer. Its __init__ takes a temperature in Celsius and stores it in an internal attribute _celsius. Use the single underscore convention for this attribute. Add a read-only property named fahrenheit that calculates and returns the temperature in Fahrenheit using the formula: (C * 9/5) + 32.
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+35 XP
Create an Inventory class. Store the value passed to __init__ in an internal attribute _count. Expose it through a count property, and add a setter that raises ValueError if the new value is negative. The @property line above a method means reading count on an inventory runs that method; @count.setter above a second method, also called count, means setting count runs that one instead.
class Inventory:
    def __init__(self, count):
        self._count = count

Run your code to check it against the tests.