Mailing List Archive

Using generator expressions
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Using generator expressions [ In reply to ]
y = test1(*[a for a in st])
y = test1(*st)
Maybe any of these would be ok for you?

Regards,
DG


> On 25 Sep 2023, at 17:15, Jonathan Gossage via Python-list <python-list@python.org> 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.
>
> --
> Jonathan Gossage
> --
> https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Using generator expressions [ In reply to ]
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
Re: Using generator expressions [ In reply 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.

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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Using generator expressions [ In reply to ]
On Tue, 26 Sept 2023 at 01:39, Jonathan Gossage via Python-list
<python-list@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
--
https://mail.python.org/mailman/listinfo/python-list