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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Joseph Martin Nograles](https://secure.gravatar.com/avatar/835aa58e6c5b95fd6db8267b0863d142?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Joseph Martin Nograles
4,696 PointsWhen passing through to html
Hi!
Two questions
Why do we use "num1" = num1 when def add while name = name when def index? Why doesn't "name" = name work?
1 Answer
![Myers Carpenter](https://secure.gravatar.com/avatar/b6b3c3d23f4181308418df6c9586acba?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Myers Carpenter
6,421 PointsSo the code in question looks like this:
def index(name="Treehouse"):
return render_template("index.html", name=name)
# ...
def add(num1, num2):
context = {"num1": num1, "num2": num2}
return render_template("index.html", **context)
To understand what's going on in add()
you need to know about keyword args and dict
s. Python allows you to pass keyword args to a callable (aka method or function). The most often you will see them like this render_template("index.html", name=name)
, where name
is a keyword arg.
Sometimes you want to make a variable that has all the keyword args you are going to pass to a callable later. That's what's going on in add()
with the context
var. Kenneth creates a dict, then later expands that dict with **context
as keyword arguments to render_template()
. If you go back in the video he shows an alternative way of writing it render_template("index.html", num1=num1, num2=num2)
.
Hope that helps!
Chris Freeman
Treehouse Moderator 68,425 PointsChris Freeman
Treehouse Moderator 68,425 PointsCan you expand your question with larger code examples?