Boardgasm.Py: builtin method overriding

So – tonight I had something that looked like:

During work today, I thought why am I making a whole second method that’s just a basic wrapper around another method?  If my intuition of Python is correct I should be able to override the method, and further, that it would be fairly trivial.

So I coded the following attempt:

Do you see the issue?  Infinite recursion.
Ok, so I’m calling the append method to the list, but the append method has to call itself to add, but then.  OK, I put it aside for a bit, and when I came back to it later, it occurred to me.

There maybe a way to call the list method by calling the list class itself without instantiating it into an object.
I don’t know why this occurred to me, but I thought you know it just seems like something Python would do…

So, I modified the above code to this:

So here, on the third line, I went from self.append(player) to list.append(self, player).

Instead of calling the custom append code (self.append) , I call the original unaltered append (which was the intended behavior all along of course).

When it worked, and I double and triple checked that it actually worked, it was one of those. Well FUCK YEAH moments.

When your intuition of what SHOULD happen and what DOES happen pleasantly aligns, and not only that, the WAY it  happens is different enough so you get to LEARN something in the process.  Now that is beautiful.  And when you see HOW it did what it did and understand it, it just.

I hate to use “elegant” because it’s overused as is, and sometimes misused.  But goddamned there is something elegant about this.

The fact I was able to logically assume a certain modus operandi w/Python and see that my assumption turned out correct and further more that it matches my perception of how something would be done in the language.

I’d say that’s pretty damn Pythonic in itself.

There’s always the particularities, and an any system you’ll run across such little pleasures that make you appreciate how something is done a certain way.

Leave a Reply

Your email address will not be published. Required fields are marked *