4 ms·
You just gave away the difference right there. x.foo doesn’t “return a bound method object.” It just accesses an attribute and returns the value of that attribu
by empthought 4y ago
You just gave away the difference right there. x.foo doesn’t “return a bound method object.” It just accesses an attribute and returns the value of that attribute. The attribute can be any Python value. If it’s a callable then it can be called with parens. There is no proxying whatsoever.
In contrast x.method(:foo) does do that proxy construction that you describe… because Ruby does not have first-class functions.
- djur 4y ago> x.foo doesn’t “return a bound method object.” It just accesses an attribute and returns the value of that attribute. The attribute can be any Python value. Python docs, section 3.2, subsection "Callable types". https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy https://docs.python.org/3/reference/datamodel.html#the-stand... x.foo returns an "instance method object", which is a "callable type". Also referred to as a "bound method", the same term that Ruby and JavaScript use. > When an instance method object is called, the underlying function (__func__) is called, inserting the class instance (__self__) in front of the argument list. For instance, when C is a class which contains a definition for a function f(), and x is an instance of C, calling x.f(1) is equivalent to calling C.f(x, 1)... Note that the transformation from function object to instance method object happens each time the attribute is retrieved from the instance... It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this only happens when the function is an attribute of the class. This last part is why the following program has an error: def foo(self): print(self) class Foo: def __init__(self, fn): self.foo = fn x = Foo(foo) x.foo()
- empthought 4y agoYou are studiously missing the point and spamming irrelevant details. Yes, there's a Python "proxy" in your docs but it is syntactically transparent. Proc's proxy is not transparent. me: > If it's a callable then it can be called with parens The docs: > These are the types to which the function call operation (see section Calls) can be applied: True of Python instance method objects. False of Ruby Methods and Procs. Therefore Python has first-class methods, and Ruby does not have first-class functions nor first-class methods. End of discussion.
- djur 4y agoIn Ruby, the function call operator, `.()`, can be used on both Method and Proc objects. This being different from the syntax used for method invocation does not have anything whatsoever to do with whether functions are first class. You are both wrong and being unreasonably rude about it.