Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How/why does the "name" submitted through the URL override the "name=Treehouse"

In the Clean URLs - Flask course:

How/why does the "name" submitted through the URL override the "name=Treehouse" that's in the argument for the index function? It's not clear to me why the page doesn't return "Treehouse" instead of the "name" submitted through the URL.

@app.route('/')
@app.route('/<name>')
def index(name="Treehouse"):
    return "Hello from {}".format(name)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Good question. The "Treehouse" default value for the parameter name is only used if a keyword argument is not presented. This is the case if the URL "/" is used to reach this view via the route "/". If the route provides a name in the URL, then the route interprets the name and route will call the view with the keyword argument name=value_from_url. This will override using the default Treehouse.

OK. So the override is built into the route method. Thanks for clearing that up for me.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

I should add it is the same override of defaults that happens whenever any Python function with a parameter with a default value is called with a keyword argument.

KAPOW! It just hit me. Thanks for the additional comment. Now I understand that this is a universal behavior for functions. So....

def hello(name="Alex"):
     print(name)


hello()        # will output "Alex"
hello('Chris') # will output "Chris"