# Python classes: a small example, line by line

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-class-basics-full-en
A class defines attributes and methods; an instance holds its own attribute values and shares the class's methods.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-43-A, TE-43-B and TE-43-C. The expected non-zero exit is TE-43-C (exit status 1). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-43-A: attributes live on the instance, methods on the class

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
printf 'class C:\n    kind = "c"\n    def __init__(self, v):\n        self.v = v\n    def show(self):\n        return self.v\na = C(1)\nb = C(2)\na.v = 9\nprint(a.v, b.v, a.kind, b.kind)\nprint(a.show.__func__ is b.show.__func__)\n' > s.py
```

Command:

```bash
python3 s.py
```

Recorded output:

```
9 2 c c
True
```

### TE-43-B: changing a class attribute is seen by every instance that has no attribute of its own, and an instance attribute hides it

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
class C:
    kind = 'c'
a = C()
b = C()
C.kind = 'z'
print(a.kind, b.kind)
a.kind = 'own'
print(a.kind, b.kind, C.kind)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
z z
own z z
```

Exit status: 0. This status and the output match what was written before the run.

### TE-43-C: expected non-zero exit: reading an attribute that the object does not have raises AttributeError

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
class C:
    pass
print(C().nosuch)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b14-bxuhpyfw/work/s.py", line 3, in <module>
    print(C().nosuch)
AttributeError: 'C' object has no attribute 'nosuch'
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-43-B: after C.kind was set to z, a.kind and b.kind both read z. After a.kind was set to own, a.kind read own, while b.kind and C.kind still read z. TE-43-C: C().nosuch stopped with exit status 1. The last line of standard error is AttributeError: 'C' object has no attribute 'nosuch'.

## Limits

Instance attributes are per object; methods are looked up on the class.

## Source

- https://docs.python.org/3/tutorial/classes.html

## Evidence

- `TE-43-A`: attributes live on the instance, methods on the class

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-43-A` | attributes live on the instance, methods on the class | 0 | PASS | `70a671c63850` |
| `TE-43-B` | changing a class attribute is seen by every instance that has no attribute of its own, and an instance attribute hides it | 0 | PASS | `4c4f222e7aad` |
| `TE-43-C` | expected non-zero exit: reading an attribute that the object does not have raises AttributeError | 1 | PASS | `fbe746d39fa1` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python sorting: sorted, sort and the key argument](https://kairos-2z6s.onrender.com/res/python-sorting-full-en)
- [Python string methods: strip, split, replace and friends](https://kairos-2z6s.onrender.com/res/python-string-methods-full-en)
- [Python comprehensions: build a list, dict or set in one line](https://kairos-2z6s.onrender.com/res/python-comprehensions-full-en)
- [Python argparse: read command-line arguments and get useful errors](https://kairos-2z6s.onrender.com/res/python-argparse-full-en)
- [Python any and all: test a whole list](https://kairos-2z6s.onrender.com/res/python-any-all-full-en)
