views:

63

answers:

3

could anyone please explain what's wrong with it ? am I doing something wrong ?

>>> class qw: 
...     def f2x(par1, par2, par3): 
...             print par1, par2, par3 
...
>>> obj = qw()
>>> obj.f2x("123", 13, "wert") Traceback (most recent call last):  
File "<stdin>", line 1, in <module>
TypeError: f2x() takes exactly 3 arguments (4 given)
>>>

if I will define just a function it's all working fine

>>> def f2x(par1, par2, par3):
...     print par1, par2, par3
... 
>>> f2x("1", 2, "too many")
1 2 too many
>>>
+3  A: 

You forgot that all member functions get another argument implicitly, which in Python is called self by convention.

Try:

class qw:
  def f2x(self, par1, par2, par3):
    print par1, par2, par3

But still call it as before:

obj = qw()
obj.f2x("123", 13, "wert")

In f2x, self is the object on which the member was called. This is a very fundamental concept of Python you should really learn about.

Eli Bendersky
+3  A: 

You need the self parameter in your class' instance method definition:

class qw:
    def f2x(self, par1, par2, par3):
        print par1, par2, par3

I'd suggest going through a beginner Python book/tutorial. The standard tutorial is good choice, especially if you already have some experience in another language.

Then you call it like so:

g = qw()
g.f2x('1', '2', '3')
Lie Ryan
@m1k3y02: what python are you using? it runs fine in my machine after adding the self parameter.
Lie Ryan
2.6.5, after re entering python command line all started to work as it is supposed to, thank you for your help
m1k3y02
+1  A: 

I guess its because of every method of a python class object implicitly has a first paramter which points to the object itself.

try

def f2x(self, par1, par2, par3):

you still call it with your 3 custom parameters

>>> class qw:
...     def f2x(self, p1, p2, p3):
...             print p1,p2,p3
... 
>>> o = qw()
>>> o.f2x(1,2,3)
1 2 3
Horst Helge
thank you all for your answers, problem solved
m1k3y02
I think you mean 'explicitly'?
Joe Holloway