diff --git a/your-first-python-program.html b/your-first-python-program.html index ac75f46..b3a3d08 100644 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -75,6 +75,51 @@ if __name__ == "__main__":

In Java and other statically-typed languages, you must specify the datatype of the function return value and each function argument. In Python, you never explicitly specify the datatype of anything. Based on what value you assign, Python keeps track of the datatype internally. +

Optional and Named Arguments

+ +

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. Futhermore, arguments can be specified in any order by using named arguments. + +

Let’s take another look at that approximate_size function declaration: + +

def approximate_size(size, a_kilobyte_is_1024_bytes=True):
+ +

The second argument, a_kilobyte_is_1024_bytes, specifies a default value of True. This means the argument is optional; you can call the function without it, and Python will act as if you had called it with True as a second parameter. + +

Now look at the bottom of the script: + +


+if __name__ == "__main__":
+    print(approximate_size(1000000000000, False))  
+    print(approximate_size(1000000000000))         
+
    +
  1. This calls the approximate_size() function with two argument. Within the approximate_size() function, a_kilobyte_is_1024_bytes will be False, since you explicitly passed False as the second argument. +
  2. This calls the approximate_size() function with only one argument. But that’s OK, because the second argument is optional! Since the caller doesn’t specify, the second argument defaults to True, as defined by the function declaration. +
+ +

You can also pass values into a function by name. + +

+>>> from humansize import approximate_size
+>>> approximate_size(4000, a_kilobyte_is_1024_bytes=False)       
+'4.0 KB'
+>>> approximate_size(size=4000, a_kilobyte_is_1024_bytes=False)  
+'4.0 KB'
+>>> approximate_size(a_kilobyte_is_1024_bytes=False, size=4000)  
+'4.0 KB'
+>>> approximate_size(a_kilobyte_is_1024_bytes=False, 4000)       
+  File "<stdin>", line 1
+SyntaxError: non-keyword arg after keyword arg
+>>> approximate_size(size=4000, False)                           
+  File "<stdin>", line 1
+SyntaxError: non-keyword arg after keyword arg
+
    +
  1. This calls the approximate_size() function with 4000 for the first argument (size) and False for the argument named a_kilobyte_is_1024_bytes. (That happens to be the second argument, but doesn’t matter, as you’ll see in a minute.) +
  2. This calls the approximate_size() function with 4000 for the argument named size and False for the argument named a_kilobyte_is_1024_bytes. (These named arguments happen to be in the same order as the arguments are listed in the function declaration, but that doesn’t matter either.) +
  3. This calls the approximate_size() function with False for the argument named a_kilobyte_is_1024_bytes and 4000 for the argument named size. (See? I told you the order didn’t matter.) +
  4. This call fails, because you have a named argument followed by an unnamed (positional) argument, and that never works. Reading the argument list from left to right, once you have a single named argument, the rest of the arguments must also be named. +
  5. This call fails too, for the same reason as the previous call. Is that surprising? After all, you passed 4000 for the argument named size, then “obviously” that False value was meant for the a_kilobyte_is_1024_bytes argument. But Python doesn’t work that way. As soon as you have a named argument, all arguments to the right of that need to be named arguments, too. +
+

Writing Readable Code

I won’t bore you with a long finger-wagging speech about the importance of documenting your code. Just know that code is written once but read many times, and the most important audience for your code is yourself, six months after writing it (i.e. after you’ve forgotten everything but need to fix something). Python makes it easy to write readable code, so take advantage of it. You’ll thank me in six months.

Documentation Strings