Nп/п : 5 из 100
 От   : Thomas Passin                       2:5075/128        25 сен 23 11:08:26
 К    : All                                                   25 сен 23 18:17:01
 Тема : Re: Using generator expressions
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<mailman.340.1695654903.23016.python-list@python.org> 5f987397
@REPLY:
<b2ba8f1e-1934-14ff-bebb-a0703f0e5eb1@tompassin.net> 577ca05f
@REPLYADDR Thomas Passin <list1@tompassin.net>
@REPLYTO 2:5075/128 Thomas Passin
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:
<mailman.340.1695654903.23016.python-list@python.org>
@RFC-References:
<CAApdmf1h9Brwre5CQGWKUEzbogdjsOettXp1BdOGHy+x6v7-wA@mail.gmail.com> <b2ba8f1e-1934-14ff-bebb-a0703f0e5eb1@tompassin.net>
@TZUTC: -0400
@PID: Mozilla/5.0 (Windows NT 10.0; Win64; x64;
rv:102.0) Gecko/20100101 Thunderbird/102.15.1
@TID: FIDOGATE-5.12-ge4e8b94
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.

 --- Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
 * 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    
                                                                                
В этой области больше нет сообщений.

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