Nп/п : 7 из 100
 От   : Jonathan Gossage                    2:5075/128        25 сен 23 11:37:16
 К    : All                                                   25 сен 23 18:43:01
 Тема : Re: Using generator expressions
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<mailman.342.1695656276.23016.python-list@python.org> 5582e5dc
@REPLY:
<CAApdmf3nnkDony12t-SbmO3OyuQ+REmgS1N-0By-qUTQM38qKA@mail.gmail.com> e6157257
@REPLYADDR Jonathan Gossage <jgossage@gmail.com>
@REPLYTO 2:5075/128 Jonathan Gossage
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:
<mailman.342.1695656276.23016.python-list@python.org>
@RFC-References:
<CAApdmf1h9Brwre5CQGWKUEzbogdjsOettXp1BdOGHy+x6v7-wA@mail.gmail.com> <b2ba8f1e-1934-14ff-bebb-a0703f0e5eb1@tompassin.net>
<CAApdmf3nnkDony12t-SbmO3OyuQ+REmgS1N-0By-qUTQM38qKA@mail.gmail.com>
@TZUTC: -0400
@TID: FIDOGATE-5.12-ge4e8b94
Many thanks, all. It turned out that my problem was not fully understanding
the use and power of the unpack operator *. Using it to activate my
generator made things start to work. I changed the line where I invoked the
generator to:
    y = test1(*(a for a in st))

adding the unpack operator.

On Mon, Sep 25, 2023 at 11:15 AM Thomas Passin via Python-list <
python-list@python.org> wrote:

> On 9/25/2023 10:15 AM, Jonathan Gossage via Python-list wrote:
> > I am having a problem using generator expressions to supply the arguments
> > for a class instance initialization. The following example shows the
> > problem:
> >
> > class test1(object):
> >      def   init  (self, a, b):
> >
> >>          self.name = a
> >
> >          self.value = b
> > st = `Programming Renaissance, Any`.split(`, `)
> > y = test1(a for a in st)
> > print(f`Object values are: {y. a}, {y. b}`)
> >
> > I would expect to get the values from the list generated by splitting the
> > string passed in as arguments to the new instance of test1, but instead
> > I get the generator expression by itself as a generator object. The
> > generator
> > expression is treated like a passive object instead of being run. If I
> had
> > wanted to pass the generator expression itself, I would have expected to
> > have
> > to use parentheses around the generator expression. Any suggestions on
> how
> > to
> > get the generator expression to run?
> > If I change the definition of the input arguments to *args I can capture
> the
> > arguments within   init   but it is verbose and ugly. Also, I could
> accept
> > the
> > arguments from a Sequence and extract the Sequence members into the class
> > values. I would prefer my solution if I could get it to work.
> > Note that I tried generator expressions both inside parentheses and not,
> > without success.
> >
>
> You should get an error at the y assignment.  The argument of test1() is
> a generator, which would get assigned to the "a" argument, and there
> would be no "b" argument, which is an error.
>
> In any event, even if this were to work as you want, it would only work
> for strings that contain one comma.  And you ask for values like y. a,
> but y. a is never created, only y.a.  If you did convert the generator
> to a list, and if you fix the underscored variable names, it still
> wouldn`t work because the arguments don`t expect a list.
>
> Time to step back and figure out exactly what you actually want to do.
>
> --
https://mail.python.org/mailman/listinfo/python-list
>


-- 
Jonathan Gossage
--- FIDOGATE 5.12-ge4e8b94
 * Origin: usenet.network (2:5075/128)
SEEN-BY: 50/109 301/1 467/888 4500/1 5000/111
5001/100 5005/49 5020/715 830
SEEN-BY: 5020/848 1042 4441 12000 5030/49 1081
5054/8 5061/133 5075/128
SEEN-BY: 5080/102 5083/1 444
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх