3 ms·
What do you mean by "unbound"? Some Python: >>> class Foo: >>> def __init__(self, x): self.x = x ... ... def bar(self): print('x =', self.x)
by twic 9y ago
What do you mean by "unbound"? Some Python:
>>> class Foo:
>>> def __init__(self, x): self.x = x
...
... def bar(self): print('x =', self.x)
...
...
>>> f = Foo(23)
>>> b = f.bar
>>> b()
x = 23
Note that when you extract the method from the object as a callable, it hangs onto the right self. Same deal as Java:
class Foo {
private int x;
Foo(int x) { this.x = x; }
void bar() { System.out.println("x = " + x); }
}
public static void main(String[] args) {
Foo f = new Foo(23);
Runnable b = f::bar;
b.run(); // prints x = 23
}
- spion 9y agoI meant "bound", sorry. Funny that you mention "::" in Java. ES7/ES8 was going to have a bind operator "::" that extracts a bound method with a slightly different syntax. i.e. `::f.bar`