Reputation: 2788
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
>>>
Upvotes: 1
Views: 1369
Reputation: 111
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
Upvotes: 2
Reputation: 64845
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')
Upvotes: 4
Reputation: 273496
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.
Upvotes: 4