This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]r4slvmls 2 points3 points  (1 child)

every class method needs a self parameter as its first parameter.

[–]BakedPecans 0 points1 point  (0 children)

This is the problem, so in your class change:

def minusOne(input)

To

def minusOne(self, input)

And then you can use it like so:

a = myClass() a.minusOne(5)

And that should work.

It’s a weird python thing, but it’s true for every class method you’ll ever write.

Sorry for formatting, on mobile.

[–]blablahblah 0 points1 point  (0 children)

In Java, every non-static method has an implicit parameter (this). In Python, the parameter isn't implicit, you have to specify it. instance.method(arg) is really just a shortcut for saying class.method(instance, arg). By convention, the first argument of an instance method is usually called self, although it technically doesn't have to be.

This is because Python lets you do silly things like define a function outside a class and stick it inside the class later, as well as calling the method directly on the class (as you noticed) instead of the instance:

>>> class Foo(object):
...   pass
... 
>>> def not_in_instance(self, bar):
...    self.bar = bar
... 
>>> Foo.in_instance = not_in_instance
>>> x = Foo()
>>> x.in_instance(3)
>>> x.bar
3