Part 4 – Web Development

Some of you have been asking about syntax I’ve used in Python basically with these asterisks in function definitions and in function calls. I’ll just show you some Python syntax that I’ve been using that is pretty helpful. One of the things we can do when we have a function definition–we’ll call this function foo– it can take a variable number of arguments. This is a handy thing you can do in Python. The way you do this is you say ” * “, and then you give it a variable name. Generally you say ” * a”. This basically says take all of the arguments passed into this function, all the unnamed arguments–we’ll come back to the named arguments in a second– and store them in a tuple, and we’ll call it “a.” That means in this function foo we have a variable a, and if I were to return a and call foo, it would look something like this. If I called foo with no arguments, it would return just an empty tuple. If I were to call foo with the arguments (1, 2, 3), it would return a tuple of (1, 2, 3). If I were to change this to return a [0], and I were to call foo with (1, 2, 3) again, It would just return the number 1. This is a handy way of making a function that can take any number of arguments. Now, I used this in class in unit 3 when we were pulling a link out of the database– When we were pulling a link out of that SQL lite database in the IDE. What you get when you pull something out of the database is just a tuple of all the columns. I can pass that tuple into the link constructor to make a link object. That’s what I was doing there. Now, something you’ve seen in my code looks something like this. I often have a function–I think in my code I have something that’s like this–def render. Oops. No paren there. It may take *a and **kw–oops, two stars. In sketchpad, they edit all these mistakes out. Generally when you see this kind of construct in Python–*a, **kw– that means they’re writing a function that adds a little bit of behavior to another function. So I have another function. I think it’s some sort of Jinja rendering thing. Oh, I actually I think what we say is template. We have a template name. It’s probably another argument in the function. We may say like t.render( *a, **kw ). Basically all this is doing is just taking the arguments from this function, the unnamed arguments and the named arguments– an named argument would be like x=1 versus an unnamed argument would just be 1– and I’m passing those directly into another function. Probably what I did here in my render function in my code is I modified this kw argument. I added some parameters to it–that sort of thing. Basically now I have this function render, which does exactly what t.render does, except I’ve added more parameters to kw. Now, there are two uses of stars and star-star. One is in the function definition, which we talked about before, and the other is in the function call. If you have a tuple or a dictionary, and you put them in a function call with a star and a **kw, it breaks those data structures apart and passes them in as parameters, which is really handy. In a function definition, it basically takes all the parameters and puts them into a tuple. In a function call, it takes a tuple and turns them into function parameters. So it’s a handy thing in Python. You see it all the time. Generally a and kw are the variable names you would use. But you can use whatever you want, and you can use them in other contexts like we did with the link database. I hope that helps. There is also, I believe, links on the forum to Python function calls that explain the syntax for you as well.

Be the first to comment

Leave a Reply

Your email address will not be published.