apply problems

Curtis Jensen cjensen at bioeng.ucsd.edu
Fri Jul 13 19:59:19 EDT 2001


William Park wrote:
> 
> On Fri, Jul 13, 2001 at 10:50:57AM -0700, Curtis Jensen wrote:
> > apply( self.ren.Elements, cmd[1], additional_kwds )
> 
> > def Elements( self, at, deformed, iact, normal, xi, nelist, nodes,
> > elements, *args, **kwds ):
> 
> > However, if I replace the apply command with:
> > self.ren.Elements( cmd[1][0], cmd[1][1], cmd[1][2], cmd[1][3],
> > cmd[1][4], cmd[1][5], additional_kwds )
> >
> > It works fine.  I can't figure why the apply line doesn't work.  There
> 
> Try
>     apply( self.ren.Elements, (self, cmd[1]), additional_kwds )
> There is subtle difference between calling Elements() as regular
> function and as class method within an object, namely 'self' argument.
> 
> --
> William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
> 8 CPUs cluster, (Slackware) Linux, Python, LaTeX, Vim, Mutt, Sc.

I thought about that.  But in the test examples below, you can see that
you'll that you don't have to pass "self" with apply.  In fact if you
do, you'll end up with too many parameters.  Also, symanticly, I think 
"apply( self.ren.Elements, (self, cmd[1]), additional_kwds )" will only
pass two arguments "self" and "cmd[1]" as apposed to sending "self" and
all the items in "cmd[1]" as individual argument.  You'd need to put
"self" at the beginning of cmd[1], or make a new tuple to pass in.  But
I know what you mean.

In any case, my problem went away.  I'm not exactly sure why.  It
probably had to do with unsafe dynamic binding or something like that.
:)

>>> class foo:
...   def __init__( self ):
...     apply( self.bar, [1,2], {'c':3, 'd':4} )
...   def bar( self, a, b, c, *args, **kwds ):
...     self.a = a
...     self.b = b
...     self.c = c
... 
>>> inst = foo()
>>> inst.a
1
>>> inst.b
2
>>> inst.c
3
>>>
>>> class foo2:
...   def __init__( self ):
...     apply( self.bar, [self,1,2], {'c':3, 'd':4} )
...   def bar( self, a, b, c, *args, **kwds ):
...     self.a = a
...     self.b = b
...     self.c = c
... 
>>> inst2 = foo2()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in __init__
TypeError: keyword parameter redefined


--
Curtis Jensen
cjensen at bioeng.ucsd.edu
http://www-bioeng.ucsd.edu/~cjensen/
FAX (425) 740-1451



More information about the Python-list mailing list