Skip to content
BENIBIX Learn

Property (Programming)

Development🧭 Programming ParadigmsObject-Oriented Programming

Also known as Object Property, Instance Variable, Field (Programming)

A named value kept in an object, like a dog's name or age.

Think of it like

It is like a name tag and a height chart for a pet. Each pet has its own.

Example

Emma's game has a player object. Its health property starts at 100 and drops each time the player is hit.

Why it matters

Properties hold the data an object knows about itself. Methods use them to do their work.

Definition

A property is a named value that belongs to an object. A dog object might have a name property and an age property. Each object keeps its own values, so two dogs can have different names. You read one with a dot, like dog.name. Some languages call them fields or attributes.

Example code

python
class Dog:
    def __init__(self, name):
        self.name = name

rex = Dog("Rex")
print(rex.name)
# Rex
Each dog gets its own name property, read with a dot.
The top part lists properties. Each dog object holds its own name.

Common mistake

Letting any code change a property at will. Then nothing stops a bug from setting age to minus five.

Key takeaways

  1. 1
    Belongs to an object. Each object has its own copy.
  2. 2
    Read with a dot. Like dog.name or player.health.
  3. 3
    Guard it. Check values before they change.