Nп/п : 3 из 100
 От   : Jonathan Gossage                    2:5075/128        25 сен 23 10:15:56
 К    : All                                                   25 сен 23 17:21:02
 Тема : Using generator expressions
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<mailman.338.1695651396.23016.python-list@python.org> 2045f718
@REPLY:
<CAApdmf1h9Brwre5CQGWKUEzbogdjsOettXp1BdOGHy+x6v7-wA@mail.gmail.com> d8662a54
@REPLYADDR Jonathan Gossage <jgossage@gmail.com>
@REPLYTO 2:5075/128 Jonathan Gossage
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:
<mailman.338.1695651396.23016.python-list@python.org>
@RFC-References:
<CAApdmf1h9Brwre5CQGWKUEzbogdjsOettXp1BdOGHy+x6v7-wA@mail.gmail.com>
@TZUTC: -0400
@TID: FIDOGATE-5.12-ge4e8b94
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.

-- 
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    
                                                                                
В этой области больше нет сообщений.

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