Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using generator expressions

13 views
Skip to first unread message

Jonathan Gossage

unread,
Sep 25, 2023, 10:17:04 AM9/25/23
to
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

Dom Grigonis

unread,
Sep 25, 2023, 10:24:22 AM9/25/23
to
y = test1(*[a for a in st])
y = test1(*st)
Maybe any of these would be ok for you?

Regards,
DG
> --
> https://mail.python.org/mailman/listinfo/python-list

Thomas Passin

unread,
Sep 25, 2023, 11:15:25 AM9/25/23
to
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.

Jonathan Gossage

unread,
Sep 25, 2023, 11:38:20 AM9/25/23
to
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.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


--
Jonathan Gossage

Chris Angelico

unread,
Sep 25, 2023, 11:43:02 AM9/25/23
to
On Tue, 26 Sept 2023 at 01:39, Jonathan Gossage via Python-list
<pytho...@python.org> wrote:
>
> 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.
>

You could simplify this with just the unpacking:

y = test1(*st)

ChrisA
0 new messages