Suggestions for python 2

Felix Thibault felixt at dicksonstreet.com
Sat Jan 22 21:48:09 EST 2000


At 22:26 1/22/00 +0000, Michael Hudson wrote:
>Felix Thibault <felixt at dicksonstreet.com> writes:
>
>> How hard would it be to implement something like:
>> 
>> def like_a_list(*now_args):
>>     def return_func(!now_args, *later_args, **show):
>>         if show:
>>             return list(now_args+later_args)
>>         else:
>>             return apply(like_a_list, now_args + later_args)
>>     return return_func
>> 
>> where the ! (or whatever) means something like this happens:
>> 
>> def like_a_list(*now_args):
>>     """like_a_list(value0, value1,...) store some values in a function"""
>>     fun_str = """def return_func(*later_args, **show):
>> 	now_args = %s
>> 	if show:
>> 		return list(now_args+later_args)
>> 	else:
>> 		return apply(like_a_list, now_args + later_args)\n"""\
>> 		 % (now_args,)
>>     exec(fun_str)
>>     return return_func
>> 
>> when the inner function is defined ? (Except that it works when
>> eval(repr(object)) doesn't and it's not as ugly). 
>
>I don't really understand what you are trying to do, but you are
>invited to check out xapply.py in my bytecodehacks package which can
>be found at
>
>http://starship.python.net/crew/mwh/
>
>and is documented here:
>
>http://starship.python.net/crew/mwh/bch/module-bytecodehacks.xapply.html
>

Thanks! I'm reading it now...

>If that isn't what you're after, can you post an example of what you
>might want to do with this hypothetical function? I can probably whip
>something up for you.

All I was really trying to do was come up with an example of a function that 
would need access to the enclosing scope and also would need *args so it
couldn't just use default argument values, but here's what I did when I tested
it:

>>> from monkeybiz import like_a_list
>>> lal123 = like_a_list(1,2,3)
>>> lal123(show=1)
[1, 2, 3]
>>> more_lal = lal123(4, 5, 6, show=1)
>>> more_lal
[1, 2, 3, 4, 5, 6]

<take some values and store them in a function that takes some more values
and checks for **kwargs, if there are any it returns its values, otherwise
it stores them in a function like itself>  

I guess what I was wondering was if there could be a syntax for faking nested
scopes like what we have with default argument passing now, except that
whatever gets passed in can't be overwritten when the function is called,
so you can use *args, or whatever, too. That's what the '!' in the first
example and the 'exec(.... % args)' that emulated it was supposed to do.
But since "def takes a name, some more names and some code with those names
and ties 'em all together" sums up what I know about what's going on...all
I really know is I'm in full agreement with:

>> >>[Evan Simpson]
>> >>>
>> >>> I rather like the idea of having to explicitly bring names
>> >>> into an inner scope,
>> >>


as-long-as-we-don't-get-real-nested-scopes-I-can-muddle-through'ly yours,

Felix

>
>Cheers,
>Michael
>-- 
>http://www.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list