4 Replies - 1215 Views - Last Post: 20 October 2010 - 04:28 AM Rate Topic: -----

#1 WTFsandwich  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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



testObject = Task2()

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  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

#3 WTFsandwich  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Class Function Argument Error

Posted 19 October 2010 - 02:59 PM

View PostWTFsandwich, 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

Was This Post Helpful? 1
  • +
  • -

#5 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1