3

I have function that looks like this:

def CreateURL(port=8082,ip_addr ='localhost',request='sth'):
    return str("http://" + ip_addr+":"+str(port) + '/' + request)

Now I want to use the default parameter for port and request but not for ip_addr. How do I have to write the function in this case?

CreateURL('192.168.2.1')

Does not work since itwill override the port and not the ip_addr

1
  • 2
    CreateURL(ip_addr='192.168.2.1') Commented Mar 14, 2017 at 12:55

4 Answers 4

6

Pass the parameter explicitly.

>>> def foo(a=1, b=2, c=3):
...     print(a, b, c)
... 
>>> foo(c=4)
(1, 2, 4)
Sign up to request clarification or add additional context in comments.

Comments

3

url = CreateURL(ip_addr='192.168.2.1')

Comments

2

Simply state the name of the parameter you wish to specify like this:

>>> CreateURL(ip_addr = '192.168.2.1')
'http://192.168.2.1:8082/sth'

Comments

1

You can simply pass parameter explicitly

>>> CreateURL(ip_addr = "192.168.2.1")
'http://192.168.2.1:8082/sth'

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.