Python calling class methods with the wrong number of parameters

Posted by Hussain on Stack Overflow See other posts from Stack Overflow or by Hussain
Published on 2010-04-13T22:59:03Z Indexed on 2010/04/13 23:03 UTC
Read the original article Hit count: 232

Filed under:
|
|
|

I'm just beginning to learn python. I wrote an example script to test OOP in python, but something very odd has happened. When I call a class method, Python is calling the function with one more parameter than given.

Here is the code:


1.  class Bar:
2.   num1,num2 = 0,0
3.   def __init__(num1,num2):
4.    num1,num2 = num1,num2
5.   def foo():
6.    if num1 > num2:
7.     print num1,'is greater than ',num2,'!'
8.    elif num1 is num2:
9.     print num1,' is equal to ',num2,'!'
10.   else:
11.    print num1,' is less than ',num2,'!'
12. a,b,t = 42,84,bar(a,b)
13. t.foo
14. 
15. t.num1 = t.num1^t.num2
16. t.num2 = t.num2^t.num1
17. t.num1 = t.num1^t.num2
18. 
19. t.foo
20. 

And the error message I get:


python test.py
Traceback (most recent call last):
  File "test.py", line 12, in 
a,b,t = 42,84,bar(a,b)
NameError: name 'bar' is not defined 

Can anyone help?
Thanks in advance

© Stack Overflow or respective owner

Related posts about python

Related posts about oop