Hacker News new | ask | show | jobs
by masklinn 5031 days ago
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)
1 comments

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.