« first day (4913 days earlier)      last day (29 days later) » 

6:41 AM
@roganjosh turns out I didn't hallucinate this but I had to get a colleague to refresh my memory on how I actually implemented it. I knew it was possible but I spent a good 30 minutes failing to recreate it. It was a really handy decorator in its own context (it doesn't make much sense in that snippet) because I had a single class to define a problem but the backend solver was switchable and not all backends supported the full interface
As usual, I must have tried every combination of func, self, *args, **kwargs in my quest and missed the one that actually works
And the more I look at it now, the less sense it makes to me on how it actually works. I don't understand why self is suddenly accessible when it isn't passed through the outer methods.
 
7:23 AM
I think what's happening is inside the class, the scope still refers to the actual function supported_backends, as opposed to bound method supported_backends which is accessible under same name on instances of the class
Unless that's not the part you're asking about
 
That is what I'm asking about, but I still don't know how self gets a value at all. How does it become a thing in the call to check_backends?
 
7:40 AM
By the same mechanism through which def add_constraint_a(self): gets self when you do problem.add_constraint_a()
problem.add_constraint_a is a bound method that, normally, would call Problem.add_constraint_a(problem) when called. But since you decorated add_constraint_a, it calls the decorated version of it, which turns out to be check_backend
 
7:57 AM
@PM2Ring happy I'm not the only one thinking this. I tried getting used to Emacs Lisp, but it's too much once there enough code.
 
 
2 hours later…
10:09 AM
@matszwecja I think my surprise is just seeing that the instance itself isn't explicitly named in the decorator that wraps the bound method. In my head, it needs to be passed through just like *args. It feels unintuitive to me but I can't really argue with the rules :P
@Aran-Fey a while back you lamented the problem with numpy that creating array masks will take a double pass on the data. Polars has just implemented branchless Boolean filters
 
It was more about the combination of multiple conditions (like mask1 | mask2), but that's pretty neat
Seeing code like df.select(pl.when(pl.col.age < cutoff).then(1).otherwise(pl.col.height).mean()) makes me wonder if there's a way to solve this reoccurring problem of "I couldn't use the actual programming language, so I had to write an interpreter for a custom mini-language" (Think numpy, pandas, polars, ORMs, etc)
 
@roganjosh But when decorating is happening, self doesn't exist. It happens when class is being initialized
So the only place where you need an actual self is when you actually call check_backends
 
10:49 AM
mm. I think I follow. It lets you write some interesting syntax; print(problem.supported_backends()(problem.add_constraint_b())); print(problem.constraints). The first print() shows a method, and adds to the constraint list, whilst print(problem.add_constraint_b()) prints None as I expect
Actually, I think I follow that output
 
11:30 AM
I'm not sure this code does what you think it does
problem.supported_backends() would call supported_backends with backends argument being self instance and return a decorator function. Which you then call with None value (since problem.add_constraint_b() returns None) and you end up with check_backends function that has func = None and backends = problem
If you try to call this function you created: problem.supported_backends()(problem.add_constraint_b())(problem) you end up with TypeError: argument of type 'Problem' is not iterable for not in backends.
What I've learned dealing with decorators is that they are really a very simple mechanism
@decorator
def foo():
    pass
is the same as
foo = decorator(foo)
The only difficulty comes down to when this happens and what else is going on, like in this case you have binding functions to instances.
 
 
7 hours later…
6:54 PM
@matszwecja heh, problem.supported_backends()(problem.add_constraint_b())(problem) was exactly the next step I took, just to see. We're on the same page :)
 
 
3 hours later…
10:18 PM
What would you call an interface that has a "to_html" method?
 
10:29 PM
@FKarlsson To give good advice I'd need more context; all I can think of is IPage and IDocument, which may not be very useful
 

« first day (4913 days earlier)      last day (29 days later) »