enriching return values / symetric call&return in P3K ? - Re: Tuple assignment and generators?
robert
no-spam at no-spam-no-spam.com
Fri May 5 04:59:13 EDT 2006
Tim Chase wrote:
> Just as a pedantic exercise to try and understand Python a bit better, I
> decided to try to make a generator or class that would allow me to
> unpack an arbitrary number of calculatible values. In this case, just
> zeros (though I just to prove whatever ends up working, having a
> counting generator would be nice). The target syntax would be something
> like
>
> >>> a,b,c = zeros()
> >>> q,r,s,t,u,v = zeros()
>
> where "zeros()" returns an appropriately sized tuple/list of zeros.
>
> I've tried a bit of googling, but all my attempts have just ended up
> pointing to pages that blithly describe tuple assignment, not the
> details of what methods are called on an object in the process.
>
> My first thought was to get it to use a generator:
>
> def zeros():
> while 1: yield 0
>
> However, I get back a "ValueError: too many values to unpack" result.
>
> As a second attempt, I tried a couple of attempts at classes (I started
> with the following example class, only derived from "object" rather than
> "list", but it didn't have any better luck):
>
> >>> class zeros(list):
> ... def __getitem__(self,i):
> ... return 0
> ...
> >>> z = zeros()
> >>> a,b,c = z
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: need more than 0 values to unpack
>
>
> It looks like I need to have a pre-defined length, but I'm having
> trouble figuring out what sorts of things need to be overridden. It
> seems like I sorta need a
>
> def __len__(self):
> return INFINITY
>
> so it doesn't choke on it. However, how to dupe the interpreter into
> really believing that the object has the desired elements is escaping
> me. Alternatively if there was a "doYouHaveThisManyElements"
> pseudo-function that was called, I could lie and always return true.
>
> Any hints on what I'm missing?
Think the use case, you present, is (pythonically) ill as such. if you
want to fill literal variable's do it like: a=b=c=...=0. Otherwise
you'd handle variable lists zeros(7)
Yet there is a real freqent need for adding transparent extra/decorating
return values in cases, where you don't want to break the simple return
scheme (in maybe big existing code). For such use cases check out this
"RICHVALUE" approach with _named_ extra values - no puzzles about
non-matching tuple assignments:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496676 :
...
def f():
return RICHVALUE(7, extra='hello')
ret=f()
print ret, ret+1, ret.extra
...
As I have this need very very often I'd like to see an even more natural
builtin method for enriching return values (both positional and by name)
in Python3000 - maybe by making calling and returning almost symetric!
Yet, this ideas maybe strange ? ...
def f_P3K(*args,**kwargs):
xreturn((7,8), 3, extra=5, *args, **kwargs) # first is main
v = f_P3K() # (7,8)
v,x = f_P3K() # (7,8),3
v,x,*pret,**kwret= f_P3K()
extra=kwret.get('extra',-1)
-robert
More information about the Python-list
mailing list