Skip to content

Commit

Permalink
Replace asserts with REPL-like syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ktnlvr committed Oct 31, 2024
1 parent 4cd9448 commit 82953b7
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions content/rambling/turing-complete-module-exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ a = Vec2(2, 3)
b = Vec2(8, 9)
c = a + b
assert c.x == 2 + 8
assert c.y == 3 + 9
>>> c.x
10
>>> c.y
12
```


One of the things that can be overloaded is member access. If the method `__getattr__` (get attribute) is defined, it will be called whenever an unset member is accessed. For instance, `x.y` is equivalent to using `x.__getattr__(y)` if no member `y` exists on the object `x`.
One of the things that can be overloaded is member access. If the method `__getattr__` (get attribute) is defined, it will be called whenever an *unset* member is accessed. For instance, `x.y` is equivalent to using `x.__getattr__(y)` if no member `y` exists on the object `x`.

```
class DictionaryObject:
Expand All @@ -69,22 +71,25 @@ dictionary = {'foo': 'bar'}
do = DictionaryObject(dictionary)
# accessing a set property
assert do._dictionary is dictionary
>>> do._dictionary is dictionary
True
# accessing the value for 'foo'
# directly with a method call
assert do.__getattr__('foo')
>>> do.__getattr__('foo')
'bar'
# `.foo` is unset
# falling back to __getattr__
# key 'foo' resolved from the dictionary
assert do.foo == 'bar'
>>> do.foo
'bar'
# `.qua` is unset
# falling back to __getattr__
# key 'qua' is not in the dictionary
# throw an error from __getattr__
assert do.qua
>>> do.qua
KeyError: 'qua'
```

Make sure to understand what exactly is happening at every step in the snippet above. Since *everything is an object*, this method can be attached to anything! Notice, how the call to `self.dictionary` does not go through `__getattr__`, because the field is defined on the object in the constructor.
Expand Down

0 comments on commit 82953b7

Please sign in to comment.