# Class Function Argument Error

Page 1 of 1

## 4 Replies - 1814 Views - Last Post: 20 October 2010 - 04:28 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=195779&amp;s=a5c75d429c9b3c36b98e6bffcb9805d7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 WTFsandwich

• New D.I.C Head

Reputation: 3
• Posts: 17
• Joined: 02-December 09

# Class Function Argument Error

Posted 19 October 2010 - 01:46 PM

```class Task2:
def zerocheck(num1, num2, num3):
if num1 == 0 or num2 == 0 or num3 == 0:
return True
else:
return False

def ordered3(num1, num2, num3):
if num1 < num2 < num3:
return True
else:
return False

def modcount(m, n):
numVal = 0
inc = 1
while inc <= n:
if inc%m == 0:
numVal += 1
inc += 1
return numVal

def helloworld(name):
print("Hello World, my name is %s", name)

def printasterisks(n):
outputStr = ""
inc = 0
if n < 0:
n = 0
if n > 75:
n = 75
while inc <= 75:
outputStr += '*'
inc += 1

print("Class functions test:\n")

firstNum = input("Enter first number for zerocheck: ")
secondNum = input("Enter second number for zerocheck: ")
thirdNum = input("Enter third number for zerocheck: ")

testObject.zerocheck(firstNum,secondNum,thirdNum)

raw_input("Press any key to continue...")

```

In the line where zerocheck is actually called, I get an exception saying I passed in 4 arguments, whereas the function is only written to accommodate 3. I fail to see how that's possible. Could someone lead me to why it would be trying to take four parameters?

EDIT: After some more testing, I noticed all functions are having this issue, not just zerocheck.

This post has been edited by WTFsandwich: 19 October 2010 - 01:54 PM

Is This A Good Question/Topic? 0

## Replies To: Class Function Argument Error

### #2 Simown

• Blue Sprat

Reputation: 322
• Posts: 650
• Joined: 20-May 10

## Re: Class Function Argument Error

Posted 19 October 2010 - 02:11 PM

In a class you need to pass "self" as an argument:

Where you defined:

```zeroCheck(num1, num2, num3)
```

As it is in the class it needs to be:

```zeroCheck(self, num1, num2, num3)
```

To give the object access to itself! Hope that helps.

### #3 WTFsandwich

• New D.I.C Head

Reputation: 3
• Posts: 17
• Joined: 02-December 09

## Re: Class Function Argument Error

Posted 19 October 2010 - 02:23 PM

I see. That solved everything. Do all functions require self as a parameter? What about functions with no arguments?

### #4 Simown

• Blue Sprat

Reputation: 322
• Posts: 650
• Joined: 20-May 10

## Re: Class Function Argument Error

Posted 19 October 2010 - 02:59 PM

WTFsandwich, on 19 October 2010 - 01:23 PM, said:

I see. That solved everything. Do all functions require self as a parameter? What about functions with no arguments?

Only functions in classes require an argument, always one:

If you wanted to say, print out "Hello!" from a class function it could be:

```class Hello:

def sayHello(self):
print "Hello!"

>>> person = Hello()
>>> person.sayHello()  #The self is assumed in this function call
Hello!

```

You need the self argument to access members of the class such as class variables.

Outside the class you do not need to use any arguments, a function with 0 arguments inside a class will require a single "self" argument.

This post has been edited by Simown: 19 October 2010 - 03:02 PM

### #5 Nallo

• D.I.C Regular

Reputation: 165
• Posts: 258
• Joined: 19-July 09

## Re: Class Function Argument Error

Posted 20 October 2010 - 04:28 AM

The reason self is required is:

```person = Hello()
person.sayHello()
```

is actually a nicer way to write
```person = Hello()
Hello.sayHello(person)
```

When you write some_instance.some_function() Python calls the some_function method of the class and passes the instance as the first parameter.

Now you may wonder why that is needed. Look at the following example of a class with 2 instances:
```class Hello():
def __init__(self, name):
self.name = name
def say_hello(self):
print("Hello World, my name is %s", self.name)

harry = Hello("Harry")
sally = Hello("Sally")

harry.say_hello()
sally.say_hello()
```

If Python didnt pass the instance along with the call Python wouldn't know whether to use harry.name or sally.name when you call harry.say_hello() (remember that is a short form for Hello.say_hello(harry))

A side note:
There is a way to put functions inside a class without the self argument. You can declare them as a staticmethod:
```>>> class Hello:
...     @staticmethod
...     def hello_world():
...         print("Hello, World")

>>> hello_instance = Hello()
>>> hello_instance.hello_world()
Hello, World

>>> Hello.hello_world()
Hello, World

```

If you come from java don't confuse Pythons staticmethod with Javas staticmethods. They are different things. A static method in Python simple means "Ok, this function lives in some class, but needs not to know anything about instances of the class (and therefore has no access to instance attributes like self.name in the example above)". Staticmethods in Python are of little practical value (unless you want to abuse classes as namespaces)

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }