can you tell me what is the error in that example

0 views
Skip to first unread message

Hatem El-Zanaty

unread,
Feb 4, 2010, 8:19:48 AM2/4/10
to utahp...@googlegroups.com
hi can you tell me what is the error in that example

#!/usr/bin/python
class example(object):
  def foo(who):       # defined for only 1 argument
    print 'Hello', who
  def test(who):
    self.foo(who)

obj=example()
obj.test("hatem")

it gives this error message

[shang1@localhost ~]$ '/data/python/function.py'
Traceback (most recent call last):
  File "/data/python/function.py", line 9, in ?
    obj.test("hatem")
TypeError: test() takes exactly 1 argument (2 given)
[shang1@localhost ~]$

how come there is 2 arguments given I passed only one in code
please help me to fix it
thanks in advance
hatem gamal

Shawn Willden

unread,
Feb 4, 2010, 8:23:06 AM2/4/10
to utahp...@googlegroups.com
The test() method only takes a single argument, "who". Calling
obj.test("hatem") calls test with two parameters, obj and "hatem".

I think you wanted to define your test method as:

def test(self, who):
self.foo(who)

The foo method also needs a self parameter.

> --
> You received this message because you are subscribed to the Google Groups
> "Utah Python User Group" group.
> To post to this group, send email to utahp...@googlegroups.com.
> To unsubscribe from this group, send email to
> utahpython+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/utahpython?hl=en.
>

--
Shawn

Adriano Monteiro Marques

unread,
Feb 4, 2010, 8:30:09 AM2/4/10
to utahp...@googlegroups.com
Hello,

The problem is that the method takes self as the first argument. self
is an argument pointing to the instance of the object from which the
method was called. To solve your problem, just do the following:

#!/usr/bin/python
class example(object):
def foo(self, who): # defined for only 1 argument
print 'Hello', who
def test(self, who):
self.foo(who)

obj=example()
obj.test("hatem")


That will do ;-)


Kind Regards,

> --
> You received this message because you are subscribed to the Google
> Groups "Utah Python User Group" group.
> To post to this group, send email to utahp...@googlegroups.com.
> To unsubscribe from this group, send email to utahpython+...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/utahpython?hl=en
> .

---
Adriano Monteiro Marques

http://www.thoughtspad.com
http://www.umitproject.org
http://blog.umitproject.org
http://www.pythonbenelux.org

"Don't stay in bed, unless you can make money in bed." - George Burns

Hatem El-Zanaty

unread,
Feb 6, 2010, 2:13:33 PM2/6/10
to utahp...@googlegroups.com
thanks all,
i now understand
Reply all
Reply to author
Forward
0 new messages