On 2022-07-20 4:45 PM, Chris Angelico wrote:
On Wed, 20 Jul 2022 at 23:50, Peter Otten <__pete...@web.de> wrote:
I found
https://peps.python.org/pep-3101/
"""
PEP 3101 – Advanced String Formatting
...
An example of the ‘getitem’ syntax:
"My name is {0[name]}".format(dict(name='Fred'))
It sh
;1'
>>> >>>
>>> >>> '{x[-1]}'.format(**vars())
>>> Traceback (most recent call last):
>>> File "", line 1, in
>>> TypeError: list indices must be integers or slices, not str
>>> >>>
>
offtopic
If you want a pure-python but definitely a more hacky implementation,
you can play around with inspect.stack() and get the variables from
the outer frames.
# code:
x = 32
y = 42
printf("Hello x={x}, y={y}", x=27)
# output:
Hello x=27, y=42
The implementation of printf() was never re
On 7/20/22 05:04, Frank Millman wrote:
> I think the preferred style these days is f'{x[-1]}' which works."
>
> Unfortunately the 'f' option does not work for me in this case, as I am
> using a string object, not a string literal.
For that you could consider
https://pypi.org/project/f-yeah/
(s
On Wed, 20 Jul 2022 at 23:50, Peter Otten <__pete...@web.de> wrote:
>
> I found
>
> https://peps.python.org/pep-3101/
>
> """
> PEP 3101 – Advanced String Formatting
> ...
> An example of the ‘getitem’ syntax:
>
> "My name is {0[name]}".format(dict(name='Fred'))
>
> It should be noted that the use
;> >>> '{x[1]}'.format(**vars())
>> '1'
>> >>>
>> >>> '{x[-1]}'.format(**vars())
>> Traceback (most recent call last):
>> File "", line 1, in
>> TypeError: list indices must b
or "license" for more information.
>>>
>>> x = list(range(10))
>>>
>>> '{x[1]}'.format(**vars())
'1'
>>>
>>> '{x[-1]}'.format(**vars())
Traceback (most recent call last):
File "", lin
On Wed, 20 Jul 2022 at 21:06, Frank Millman wrote:
> I saw this from Paul Rubin - for some reason his posts appear in google
> groups, but not python-list.
>
> "It seems to only want integer constants. x[2+2] and x[k] where k=2
> don't work either.
Yes, that's for the same reason that x[spam] can
>>>
> >> >>> '{x[1]}'.format(**vars())
> >> '1'
> >> >>>
> >> >>> '{x[-1]}'.format(**vars())
> >> Traceback (most recent call last):
> >> File "", l
;, "copyright", "credits" or "license" for more information.
>>>
>>> x = list(range(10))
>>>
>>> '{x[1]}'.format(**vars())
'1'
>>>
>>> '{x[-1]}'.format(**vars())
Traceback
or "license" for more information.
>>>
>>> x = list(range(10))
>>>
>>> '{x[1]}'.format(**vars())
'1'
>>>
>>> '{x[-1]}'.format(**vars())
Traceback (most recent call last):
File ""
ense" for more information.
> >>>
> >>> x = list(range(10))
> >>>
> >>> '{x[1]}'.format(**vars())
> '1'
> >>>
> >>> '{x[-1]}'.format(**vars())
> Traceback (most recent call last):
>
>
>>> '{x[1]}'.format(**vars())
'1'
>>>
>>> '{x[-1]}'.format(**vars())
Traceback (most recent call last):
File "", line 1, in
TypeError: list indices must be integers or slices, not str
>>>
Can anyone explain this
13 matches
Mail list logo