Learning LibraryCore Coding LibraryTeens

Methods and Attributes in Python: an object's data and actions

Every object carries two kinds of things: attributes — the data it holds — and methods — the actions it can perform. An enemy has a position and can move.

The big idea

An attribute is a value stored on an object (obj.name); a method is a function that belongs to it and is called with () (obj.action()).

See it in code

1The basics

Every value in Python carries methods — actions it can perform, called with parentheses. A string's .upper() returns an all-caps copy:

python
name = "nova"
print(name.upper())
Run it — the method does something to the string:
NOVA

.upper() is a method, so it needs the () to run. That's the rule: parentheses mean 'do the action'.

2A step further

Objects also carry attributes — data you read without parentheses. This Player has a score attribute and an add_point method; calling the method changes the attribute:

python
class Player:
    def __init__(self):
        self.score = 0

    def add_point(self):
        self.score = self.score + 1

p = Player()
print("score attribute:", p.score)
p.add_point()
p.add_point()
print("after two add_point() calls:", p.score)
Run it — reading data vs performing an action:
score attribute: 0
after two add_point() calls: 2

p.score reads data — no parentheses. p.add_point() performs an action — parentheses required. Two calls nudged score from 0 to 2.

3In our world

Now a real game object. An Enemy shows both clearly: enemy.y is an attribute — data you can read — and enemy.move_down() is a method, an action that needs the parentheses. Two calls change the attribute:

python
from game import Stage, Enemy

screen = Stage.create()
Stage.clear(screen)

enemy = Enemy(240, 100)
print("Start y (attribute):", enemy.y)

enemy.move_down()
enemy.move_down()
print("After two move_down() calls:", enemy.y)

enemy.draw(screen)
Run it — the method changes the attribute:
Start y (attribute): 100
After two move_down() calls: 102
A single purple enemy sprite near the top of a dark play area.

Reading enemy.y needs no parentheses — it's a value. Calling enemy.move_down() needs them — it's an action. Each move_down() nudged the y attribute down by the enemy's speed, from 100 to 102.

The same idea, everywhere

This dot-syntax split is everywhere in Python. A string's .upper() is a method, a list's .append() is a method; a DataFrame's .shape is an attribute, its .sort_values() a method. The rule of thumb: parentheses mean 'do something', no parentheses means 'read something'.

Try it yourself

Read enemy.x and enemy.speed (attributes), then set enemy.speed = 5 and call move_down() — watch y jump by 5. Notice enemy.move_down without () gives you the method object itself, not the result.

The common mistake

Mixing up when to use (). Writing enemy.move_down (no parentheses) references the method but never runs it, so nothing moves; writing enemy.y() tries to call a number and raises a TypeError. Parentheses call; bare names read.

What it unlocks

Methods and attributes are the surface of classes and objects, and explain how sprites and entities hold state and act on it.