by BiteCode_dev · 5y ago · 0 comments
Expr = Callable[[str]], Callable[..., Iterable[str]]]
AstNode = Expr | Callable[... ], Callable[..., Iterable[str]]]
You then don't event need to call representation() , as each node is callable. No classes needed.But my instinct tells me a node will eventually have more attributes and methods. Stuff to get parents, children, siblings, filtering and so on.
So it's probably a good call to use a class for later. Also you want to keep AstNode as a node could be an expression, a statement or a comment.
I couldn't agree more with Jack Diederich's thoughts on that sentiment, at 5:40 in the video linked above:
https://youtu.be/o9pEzgHorH0?t=340
> Lots of times people think they might need something later. You don't. Or, you can just do it later.
Experience helps with this.
It's a middle path as usual: naive approaches waste as much time as overenginering.
Maybe this is an indictment of Python as a language, but I think it's a case of not taking anything too literally. YAGNI fetishism is like DRY fetishism all over again.
My sense is that many of us are taught to think in terms of classes and methods instead of data structures and functions, so it's very easy to slip into thinking you need objects in order to manage state, even when that's not the simplest way to do it.
Strictly speaking, though, I think that the only time in Python that you really have no workable alternative to classes is when you also need dynamic dispatch.
If you lose either of those, a little bit of preventative work goes a loooooong way. The friction (largely due to risk) of correcting things later is so large that it just starts snowballing unless your team is extremely attentive.
In that sense, Python is... middle of the road, with an extremely broad spread. Tooling is an odd blend of surprisingly good vs incapable of doing simple inference / detecting all calls, and type safety depends heavily on how you write things (some tactics are fairly strong, many are so weak as to be useless or make things much worse).
* I could have said refactoring, but some people have very specific definitions attached to that word so I use something more general here