> Another response from a different source. Having to include self as a method argument means that those are not methods, but static functions.
This declaration makes no sense.
> This really means there isn't precisely methods at all
Of course there is:
>>> class Foo(object):
... def bar(self): return 42
...
>>> type(Foo().bar)
<type 'instancemethod'>
>>> m = Foo().bar
>>> m()
42
> If there is a valid reason for this, it would go a long way in explaining away one of the major reasons I dislike python.I don't know (and don't really care) if you'll consider it "a valid reason", but here's GvR's reasoning: http://neopythonic.blogspot.be/2008/10/why-explicit-self-has...
Functions don't magically become methods just because the language hid the "self" or "this" parameter. You're focusing on irrelevant details.
It's not that the language is hiding self/this so much as what manually passing those as variables represent. What you are saying is that you never need to call a method like this?
> instance.method(instance)
Re:What are the drawbacks of Python?
by majormajor on Saturday September 08, 2012 @11:56AM
(#4493173)
You only have to list self in the declaration, you don't have to pass the instance to itself.
Borrowing masklinn's example:
>>> class Foo(object):
... def __init__(self):
... self.n = 42
... def bar(self):
... return self.n
...
>>> f = Foo()
>>> f.bar()
42Although you can also (and that can be rather neat when e.g. mapping or filtering) use the method as a function, if you get it from the class:
f = Foo()
Foo.bar(f)
note that it will typecheck the first argument to ensure it's an instance of `Foo`, it's not a function, it's an unbound method: >>> class Bar(object): pass
...
>>> b = Bar()
>>> b.n = 66
>>> Foo.bar(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method bar() must be called with Foo instance as first argument (got Bar instance instead)
Re:What are the drawbacks of Python?
by mattdeboard on Saturday September 08, 2012 @01:14PM
(#4493367)
If you want to do that use the @staticmethod decorator.
Oh just, you know, create a function... There's nothing more pointless than @staticmethod, unless it was used specifically to port code it should mark whoever used it for a whipping.
Re:What are the drawbacks of Python?
by eternalban on Saturday September 08, 2012 @11:03AM
(#4493054)
> "... python appears hacked together ..."
I had to extract that as a concise summation of my subjective view of the language as a whole. (Note: hacked != bad.)
If there is a valid reason for this, it would go a long way in explaining away one of the major reasons I dislike python.
Edit: Appears to be a bug and I cannot respond to your post 'masklinn'. Can you rework the example to include the result as 42 being an instance variable instead? Thanks.