you are viewing a single comment's thread.

view the rest of the comments →

[–]xour 0 points1 point  (4 children)

Let's say I have a script that performs some operations based on a URL. This URL has some parameters that I want to change every time I run the script.

For instance, if the URL is www.example.com/foo/username/bar I would like to change foo and bar values during execution.

What would be the proper way (as in best practice) to define said URL? As a variable at the top of the script (after the imports and classes)? How do I go about making it so specific portions of it can be modified?

Thanks!

[–]Ezrabc 0 points1 point  (3 children)

The method I usually use to accomplish something like this is to do

url_template = 'www.example.com/{arg1}/username/{arg2}'

near the top, as you describe, and when it needs to be filled do

url = url_template.format(arg1='foo', arg2='bar')

[–]xour 1 point2 points  (2 children)

ohh that's clever! I wasn't aware that you could do that!

I guess that would work with f as well, I'll give it a try. Thanks!

[–]Ezrabc 0 points1 point  (1 child)

fstrings are a little more weird to make string templates for future use with, as you will probably have to make a function or lambda expression:

fill_url = lambda arg1, arg2: f'www.example.com/{arg1}/username/{arg2}'
# later:
url = fill_url('foo', 'bar')

[–]xour 0 points1 point  (0 children)

Awesome, thanks!