[PEAK] Re: generic functions and keyword arguments

Phillip J. Eby pje at telecommunity.com
Tue Aug 5 09:53:28 EDT 2008


At 11:41 AM 8/5/2008 +0300, Vladimir Iliev wrote:
>Phillip J. Eby написа:
>>At 09:57 AM 8/4/2008 +0300, Vladimir Iliev wrote:
>>>Phillip J. Eby напиÃ'Ð°:
>>>>At 05:14 PM 8/2/2008 +0300, Vladimir Iliev wrote:
>>>>>hi, can i somehow use keyword arguments with peak.rules based 
>>>>>generic functions ?
>>>>Arguments passed by keyword are treated the same as arguments 
>>>>passed by position, so e.g.:
>>>>  def a(b, c):
>>>>      ...
>>>>can be called with "a(c=1,b=2)".   If you mean, can you do this:
>>>>
>>>>   def a(**kw):
>>>>and then test for whether the caller passed in b=2, you would use 
>>>>rules like:
>>>>   @when(a, "'b' in kw and kw['b']==2")
>>>>in order to dispatch on arguments that are not part of the 
>>>>generic function's "def" signature.
>>>
>>>no, i just want to pass arguments that are not part of the 
>>>signature through the gf.
>>>
>>>if i do this:
>>>
>>>@abstract
>>>def func(x, **k):
>>>     pass
>>>
>>>@when(func, (str,))
>>>def func_str(x, **k):
>>>     print 'str'
>>>
>>>
>>>func('x', y=5)
>>>
>>>i'm getting TypeError: callback() got an unexpected keyword argument 'y'
>>Hmm.  I'm getting this:
>>Python 2.5.1 (r251:54863, Nov 28 2007, 17:53:19) [MSC v.1310 32 bit 
>>(Intel)] on
>>win32
>>Type "help", "copyright", "credits" or "license" for more information.
>>  >>> from peak.rules import *
>>  >>> @abstract
>>... def func(x, **k): pass
>>...
>>  >>> @when(func,(str,))
>>... def fstr(x,**k): print 'str'
>>...
>>  >>> func('x')
>>str
>>  >>> func('x',y=42)
>>str
>>  >>> func('x', y=5)
>>str
>>  >>>
>>Are you sure that's all the code needed for you to reproduce the problem?
>
>yes. i didn't mentioned that i'm using python 2.4

Python 2.4 (#60, Dec 15 2004, 22:59:59) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> from peak.rules import *
 >>> @abstract
... def func(x, **k): pass
...
 >>> @when(func,(str,))
... def fstr(x,**k): print 'str'
...
 >>> func('x')
str
 >>> func('x',y=42)
str
 >>> func('x', y=5)
str
 >>>




More information about the PEAK mailing list