[Tutor] apply help (newbie)

karthik Guru karthikg@aztec.soft.net
Sat, 15 Dec 2001 23:00:32 +0530


thanks for the explanation.
This is what i understood..please correct me if i'm wrong.

i did a

print type(*args) => turned out to be an instance of something
print type(args) => a tuple as expected.

so when i say ,

QPushButton.__init__(self,args)    # i'm passing the tuple directly rather
than the instance *args

so it failed as QPushButton does not take a tuple but instead requires
that instance (*args) in this case.

>>> def func(*args):
...     for arg in args:
...             print arg
...
>>>
>>> def call(*args):
...     func(args)
...
>>> call(1,2,3,4)
(1, 2, 3, 4)
>>> def call(*args):
...     func(*args)
...
>>> call(1,2,3,4)
1
2
3
4
>>> def call(*args):
...     apply(func,args)
...
>>> call(1,2,3,4)
1
2
3
4
>>> def call(*args):
...     apply(func,*args)
...
>>> call(1,2,3,4)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in call
TypeError: apply() takes at most 3 arguments (5 given)
>>>

*args is instance of which class?
why do i get a type error in the end when i try passing that instance to
func?
it's saying am passing 5 arguments when in reality am passing only one (ie
*args) ?

thanks ,
karthik.


-----Original Message-----
From: Sean Perry [mailto:shaleh@one.local]On Behalf Of Sean 'Shaleh'
Perry
Sent: Saturday, December 15, 2001 10:07 PM
To: karthik Guru
Cc: tutor@python.org
Subject: Re: [Tutor] apply help (newbie)


>
> can someone tell me as to what is the difference between these 2 calls:
>
>               QPushButton.__init__(self,args)         ...1

args is a tuple containing the options passed to Button.__init__ QPushButton
does not take a tuple as its arguments I suspect

>               apply(QPushButton.__init__,(self,)+args)  ...2

passes a tuple (self, args) to QPushButton.__init__ via apply which turns
the
arg tuple into the arguments QPushButton is expecting.  In python 2.1 and
newer
you can call

QPushButton.__init__(self, *args) # pass argument tuple through to function.

In 1.x you have only apply() to use.