Hacker News new | ask | show | jobs
by mazatta 3335 days ago
I've worked on codebases that use a similar thing, and I have yet to see one where this wasn't a mistake.

Most of the time, this probably gives you exactly what you want, but then there are times where you discover bugs in production because data you assumed is your custom class is a plain old dict, and now you're raising AttributeError all over the place. Another wart is if you are unfortunate enough to have keys that match the name of one of dict's methods, then you have to resort to instance['items'], which defeats the purpose of using this in the first place.

This is a fun trick, but if someone one my team tried to introduce this, it won't make it through code review.

2 comments

I agree, but when I saw this, my first assumption was that it was for personal projects primarily. I very rarely see libraries that change syntax like this in large scale or long term codebases.
I agree, I had a legacy code base with this as well. It was a giant pain both in prod as you say but also for green developers. Really slowed down development.