Re:What are the drawbacks of Python?
by jemfinch on Saturday September 08, 2012 @11:00AM
(#4493049)
Functions don't magically become methods just because the language hid the "self" or "this" parameter. You're focusing on irrelevant details.
[ Parent
| on HN ]
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.