Hacker News new | ask | show | jobs
by nahname 5031 days ago
Another response from a different source. Having to include self as a method argument means that those are not methods, but static functions. This really means there isn't precisely methods at all and the whole OOP part of python appears hacked together. That part seriously turned me off when I encountered it.

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.

3 comments

> 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)

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()
  42
Although 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)
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.
> "... 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.)