3

Consider the following snippet:

>>> def foo(port, out, udp=False, ipv6=False, data=''):
...     if not data:
...             data = 'foo {family} {:port} {direction}'.format(
...                             family=('ipv6' if ipv6 else 'ipv4'),
...                             port=port,
...                             direction=('out' if out else 'in'))
...     return data
...
>>> foo(12345, out=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in foo
IndexError: tuple index out of range

As far as I know, the scoping of names look alright. What's with the cryptic error?

1
  • Nevermind. {:port} should be {port:}. Commented Sep 16, 2010 at 0:10

2 Answers 2

2

Watch the colon. Move it from the front of the port area:

Either

data = 'foo {family} {port:} {direction}'.format(

Or

data = 'foo {family} :{port} {direction}'.format(

The results of the two options are:

>>> foo(12345, out=True)
'foo ipv4 12345 out'
>>> foo(12345, out=True)
'foo ipv4 :12345 out'    
Sign up to request clarification or add additional context in comments.

2 Comments

Noted. <pedantic>They're called "colons"</pedantic>. ;-)
That's not pedantic at all :) Remedied.
0

{:port} should be {port:}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.