Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33820

Re: Yet another Python textbook

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Yet another Python textbook
Date 2012-11-22 17:12 -0500
References (4 earlier) <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <mailman.96.1353445247.29569.python-list@python.org> <50AD0962.5080002@ncf.ca> <mailman.180.1353536254.29569.python-list@python.org> <50AE1986.2080605@ncf.ca>
Newsgroups comp.lang.python
Message-ID <mailman.214.1353622392.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 11/22/2012 7:24 AM, Colin J. Williams wrote:

>  From my reading of the docs, it seems to me that the three following
> should be equivalent:

We read differently...
>
>    (a) formattingStr.format(values)

Where 'values' is multiple arguments

> with
>    (b) format(values, formattingStr)

"format(value[, format_spec])
Convert a value to a “formatted” representation, as controlled by 
format_spec."

I notice that you did not pass multiple args, but indeed just one.
A 'format_spec' is only part of a {} formatting field.

> or
>    (c) tupleOfValues.__format__(formattingStr

 >>> tuple.__format__
<method '__format__' of 'object' objects>

Which of to say, not specific to tuples.

> Example:
> print('{:-^14f}{:^14d}'.format(-25.61, 95 ))
> print(format((-25.61, 95), '{:-^14f}{:^14d}'))

"The interpretation of format_spec will depend on the type of the value 
argument, however there is a standard formatting syntax that is used by 
most built-in types: Format Specification Mini-Language." (The latter is 
link to the FSML.

'-^14f' and '^14d' are format_specs.
'{:-^14f}{:^14d}' is a format string that includes two fields with 
format specs. It is not a format spec in itself and is therefore invalid 
by the doc.

> (-25.61, 95 ).__format__('{:-^14f}{:^14d}')
>
> The second fails, perhaps because values can only be a single value.

You only passed one, but you did not pass a format spec and indeed there 
is none for tuples. As delivered, format specs only format strings and 
numbers as strings. Collection classes other than str recursively format 
their members using str() or repr() until they reach strings, numbers, 
or customs class instances with custom .__format__ methods.

> Should we retreat to %-formatting for now?

Nonsense. The issues above are the same for % formatting. If you try to 
format one object with two % format specs, it will fail for the same 
reason. Try the % equivalent of what failed.

'%-14f%14d' % ((-25.61, 95 ),)

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-20 19:09 +1100
  Re: Yet another Python textbook wxjmfauth@gmail.com - 2012-11-20 06:57 -0800
    Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-21 08:00 +1100
      Re: Yet another Python textbook wxjmfauth@gmail.com - 2012-11-21 06:49 -0800
      Re: Yet another Python textbook wxjmfauth@gmail.com - 2012-11-21 06:49 -0800
      Re: Yet another Python textbook "Colin J. Williams" <cjw@ncf.ca> - 2012-11-21 12:03 -0500
        Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-22 09:17 +1100
          Re: Yet another Python textbook "Colin J. Williams" <cjw@ncf.ca> - 2012-11-22 07:24 -0500
            Re: Yet another Python textbook Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-22 11:27 -0700
              Re: Yet another Python textbook "Colin J. Williams" <cjw@ncf.ca> - 2012-11-22 17:41 -0500
                Re: Yet another Python textbook Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-23 03:26 +0000
            Re: Yet another Python textbook Terry Reedy <tjreedy@udel.edu> - 2012-11-22 17:12 -0500
        Re: Yet another Python textbook Dave Angel <d@davea.name> - 2012-11-21 17:58 -0500
        Re: Yet another Python textbook Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-21 16:11 -0700
        Re: Yet another Python textbook Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-21 23:26 +0000
        Re: Yet another Python textbook Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-21 23:32 +0000
        Re: Yet another Python textbook Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-21 17:19 -0700
        Re: Yet another Python textbook Terry Reedy <tjreedy@udel.edu> - 2012-11-21 23:04 -0500
    Re: Yet another Python textbook Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-20 21:55 +0000
    Re: Yet another Python textbook Chris Angelico <rosuav@gmail.com> - 2012-11-21 09:25 +1100

csiph-web