error in syntax description for comprehensions?

2017-03-30 Thread Boylan, Ross
https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries
describes the syntax for comprehensions as
comprehension ::=  expression comp_for
comp_for  ::=  [ASYNC] "for" target_list "in" or_test [comp_iter]
comp_iter ::=  comp_for | comp_if
comp_if   ::=  "if" expression_nocond [comp_iter]

Is the comp_for missing an argument after "in"?
One has to follow the definition of or_test and its components, but I can't 
find anything that results to a single variable or expression.

Actually, I'm not sure what or_test would do there either with or  without an 
additional element following "in". 

Ross Boylan
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: how to control formatting of a namedtuple in a list

2016-11-17 Thread Boylan, Ross
Actually,
>> print(list(foo, bar))
Traceback (most recent call last):  

  File "", line 1, in

TypeError: list() takes at most 1 argument (2 given)   

Though 
>>> [foo, bar]
['ham, eggs, cheese', 'bacon, toast'] 
which admittedly would be confusing if the strings weren't quoted. But display, 
or formatted values, are not intended to allow reconstruction of the objects; 
that's what repr is for. 

The argument that the display of list is already low-level, and so everything 
inside the list should be displayed low-level, seems a bit of a stretch.

Overriding repr is serviceable for me, but it requires violating the intended 
semantics of repr, namely that the result could be converted back to the 
original object. I'm trying to get a display that has only some of the 
information in the object.  My understanding is that str is supposed to provide 
that.

At any rate, I agree there are reasons to use repr inside a list.

Ross

From: Python-list [python-list-bounces+ross.boylan=ucsf@python.org] on 
behalf of Ethan Furman [et...@stoneleaf.us]
Sent: Thursday, November 17, 2016 4:18 PM
To: python-list@python.org
Subject: Re: how to control formatting of a namedtuple in a list

On 11/17/2016 04:09 PM, MRAB wrote:
> On 2016-11-17 23:49, Boylan, Ross wrote:

>> Thank you; I can confirm that overriding __repr__ makes the list display as 
>> I wanted.
>>
>> The decision to use repr inside the list seems very odd, given the context, 
>> namely formatting something for display or looking for a simple string 
>> representation.  It seems more natural to me to use str or, if in a format, 
>> the default formatting all the way down.  Is there a good reason it's repr?
>
> Given a string, say:
>
> >>> s = 'foo'
>
> str shows:
>
> >>> print(str(s))
>
> whereas repr shows:
>
> >>> print(repr(s))
> 'foo'
>
> If it was in a list, would you want it to show:
>
> [foo]
>
> or:
>
> ['foo']
>
> ?

Another example:

  >>> foo = 'ham, eggs, cheese'
  >>> bar = 'bacon, toast'

if list used str instead of repr:

  >>> print(list(foo, bar))
  [ham, eegs, cheese, bacon, toast]

How many items are in that list?  (Hint: it isn't 5. ;)

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: how to control formatting of a namedtuple in a list

2016-11-17 Thread Boylan, Ross
Thank you; I can confirm that overriding __repr__ makes the list display as I 
wanted.

The decision to use repr inside the list seems very odd, given the context, 
namely formatting something for display or looking for a simple string 
representation.  It seems more natural to me to use str or, if in a format, the 
default formatting all the way down.  Is there a good reason it's repr?

Ross

From: Python-list [python-list-bounces+ross.boylan=ucsf@python.org] on 
behalf of Chris Angelico [ros...@gmail.com]
Sent: Thursday, November 17, 2016 3:24 PM
To: python-list@python.org
Subject: Re: how to control formatting of a namedtuple in a list

On Fri, Nov 18, 2016 at 10:04 AM, Boylan, Ross <ross.boy...@ucsf.edu> wrote:
> Even after defining custom __str__ and __format__ methods they don't affect 
> the display of objects when they are in a list.  Is there a way to change 
> that, other than explicitly converting each list element to a string?
>

Yep! Inside a list, it's the repr that gets shown. So you should be
able to do this:

class Foo(namedtuple("Foo", "x")):
def __repr__(self):
  return "foolish({})".format(self.x)

This will also affect the other forms - if you don't define __str__,
it'll use __repr__. So this should be all you need.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


how to control formatting of a namedtuple in a list

2016-11-17 Thread Boylan, Ross
Even after defining custom __str__ and __format__ methods they don't affect the 
display of objects when they are in a list.  Is there a way to change that, 
other than explicitly converting each list element to a string?

The last line of output below shows that when I format the list I get standard 
formatting of my objects instead of my custom format.

Code
#! /usr/bin/python3
from collections import namedtuple

class Foo(namedtuple("Foo", "x")):
__slots__ = ()

def __str__(self):
  return "foolish({})".format(self.x)

def __format__(self, spec):
  return self.__str__()

f=Foo(4)
print(f)
print(str(f))
print("{}".format(f))
print("{}".format([f]))  # a list with one f

Output
foolish(4)
foolish(4)
foolish(4)
[Foo(x=4)]

I'm running Python 3.4.

Thanks.
Ross
-- 
https://mail.python.org/mailman/listinfo/python-list