Python classes: a small example, line by line
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.
TE-43-A: attributes live on the instance, methods on the class
Setup for this run, inside a throwaway directory (mktemp -d):
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.pyCommand:
python3 s.pyRecorded output:
9 2 c c
TrueTE-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):
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)
EOSCommand:
python3 s.pyRecorded output:
z z
own z zExit 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):
cat > s.py <<'EOS'
class C:
pass
print(C().nosuch)
EOSCommand:
python3 s.pyRecorded 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
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.
All full guides: All guides