![]() ![]() When Python’s parser sees something like “t = (10)”, it can’t know that we’re talking about a tuple. What surprises many newcomers to Python is the following: In : t = (10) Which means that when you return multiple values from a function, you’re actually returning a tuple: In : def foo(): What many beginning Python developers don’t know is that you actually don’t need the parentheses to create the tuple: In : t = 10,20,30 Of course, we can also use () to create tuples. We can change the priority by using round parentheses: In : (2 + 3) * 4Įxperienced developers often forget that we can use parentheses in this way, as well - but this is, in many ways, the most obvious and natural way for them to be used by new developers. Python clearly went to elementary school as well, because it follows this order. In elementary school, you probably learned the basic order of arithmetic operations - that first we multiply and divide, and only after do we add and subtract. In both of the above cases, we don’t want to get help on the output of those functions rather, we want to get help on the functions themselves. One example is when we’re in the Jupyter notebook (or other interactive Python environment) and ask for help on a function or method: help(len) I should note that we also need to be careful in the other direction: Sometimes, we want to pass a function as an argument, and not execute it. Once we do that, we get the desired result. For example, I see the following code all the time in my courses: d = It’s worth considering what happens if you don’t use parentheses. Perhaps the most obvious use for parentheses in Python is for calling functions and creating new objects. Regular parentheses - () Callables (functions and classes) On no small number of occasions, I’ve been able to find bugs quickly thanks to the paren-coloring system in Emacs. I should also note that the large number of parentheses that we use in Python means that using an editor that colorizes both matching and mismatched parentheses can really help. If you’re new to Python, then I hope that this will help to give you a clearer picture of what is used when. I’ve thus tried to summarize each of these types of parentheses, when we use them, and where you might get a surprise as a result. But to newcomers, it’s far from obvious when to use round parentheses, square brackets, and/or curly braces. ![]() To many developers, and especially Python developers, it’s obvious not only that there are different types of parentheses in Python, but that each type has multiple uses, and do completely different things. Indeed, that’s almost the definition of an expert - someone who understands a subject so well, that for them things are obvious. It’s thus super easy to take things for granted when you’re an expert. Similarly, I learned all sorts of rules for Hebrew grammar that my children never learned in school. I grew up speaking English, and never learned all sorts of rules that my non-native-speaking friends learned in school. You can often see and experience this when you compare how you learn a language as a native speaker, from how you learn as a second language. Things that we take for granted in our day-to-day lives, and which seem so obvious to us, take a long time to master. ![]() If you’re like me, you were probably amazed by how long it took to do things that we don’t even think about. If you have children, then you probably remember them learning to walk, and then to read.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |