- In fact, every Python function returns a value; if the function ever executes a
return
statement, it will return that value, otherwise it will returnNone
, the Python null value - Reading the argument list from left to right, once you have a single named argument, the rest of the arguments must also be named.
sys.path
is a list of directory names that constitute the current search path. (Yours will look different, depending on your operating system, what version of Python you’re running, and where it was originally installed.) Python will look through these directories (in this order) for a.py
file whose name matches what you’re trying to import.- Everything between the triple quotes is the function’s
docstring
, which documents what the function does. We can look the doc by .__doc__ - All functions have a built-in attribute
__doc__
, which returns the docstring defined in the function’s source code - In Python, functions are first-class objects. You can pass a function as an argument to another function. Modules are first-class objects. You can pass an entire module as an argument to a function. Classes are first-class objects, and individual instances of a class are also first-class objects
- everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Classes are objects. Class instances are objects. Even modules are objects.
- n reality, exceptions are implemented as classes, and this
raise
statement is actually creating an instance of theValueError
class and passing the string'number must be non-negative'
to its initialization method. - You don’t need to handle an exception in the function that raises it. If one function doesn’t handle it, the exception is passed to the calling function, then that function’s calling function, and so on “up the stack.” If the exception is never handled, your program will crash, Python will print a “traceback” to standard error, and that’s the end of that. Again, maybe that’s what you want; it depends on what your program does.
- You don’t need to handle an exception in the function that raises it. If one function doesn’t handle it, the exception is passed to the calling function, then that function’s calling function, and so on “up the stack.” If the exception is never handled, your program will crash, Python will print a “traceback” to standard error, and that’s the end of that. Again, maybe that’s what you want; it depends on what your program does.
-
try: from lxml import etree except ImportError: import xml.etree.ElementTree as etree
- All names in Python are case-sensitive: variable names, function names, class names, module names, exception names. If you can get it, set it, call it, construct it, import it, or raise it, it’s case-sensitive.
- The
+
operator concatenates lists to create a new list. A list can contain any number of items; there is no size limit (other than available memory). However, if memory is a concern, you should be aware that list concatenation creates a second list in memory. In this case, that new list is immediately assigned to the existing variable a_list. So this line of code is really a two-step process — concatenation then assignment — which can (temporarily) consume a lot of memory when you’re dealing with large lists. - A list can contain items of any datatype, and the items in a single list don’t all need to be the same type. Here we have a list containing a string, a floating point number, and an integer.
- The
extend()
method takes a single argument, which is always a list, and adds each of the items of that list to a_list. - On the other hand, the
append()
method takes a single argument, which can be any datatype. Here, you’re calling theappend()
method with a list of three items. - That’s right: the
index()
method raises an exception if it doesn’t find the value in the list. - You can use the
del
statement to delete a specific item from a list. - You can also remove an item from a list with the
remove()
method. - When called without arguments, the
pop()
list method removes the last item in the list and returns the value it removed.You can pop arbitrary items from a list. Just pass a positional index to thepop()
method. It will remove that item, shift all the items after it to “fill the gap,” and return the value it removed. - A tuple is an immutable list. A tuple can not be changed in any way once it is created.
- Tuples are faster than lists. If you’re defining a constant set of values and all you’re ever going to do with it is iterate through it, use a tuple instead of a list.
- It makes your code safer if you “write-protect” data that doesn’t need to be changed. Using a tuple instead of a list is like having an implied
assert
statement that shows this data is constant, and that special thought (and a specific function) is required to override that. - Some tuples can be used as dictionary keys (specifically, tuples that contain immutable values like strings, numbers, and other tuples). Lists can never be used as dictionary keys, because lists are not immutable.
- To create a tuple of one item, you need a comma after the value. Without the comma, Python just assumes you have an extra pair of parentheses, which is harmless, but it doesn’t create a tuple
- In Python, you can use a tuple to assign multiple values at once.
- As I mentioned earlier, a single set can contain values of any datatype. And, as I mentioned earlier, sets are unordered. This set does not remember the original order of the list that was used to create it. If you were to add items to this set, it would not remember the order in which you added them.
- The printed representation of an empty set looks a bit strange. Were you expecting
{}
, perhaps? That would denote an empty dictionary, not an empty set. You’ll learn about dictionaries later in this chapter. - The
update()
method can take objects of a number of different datatypes, including lists. When called with a list, theupdate()
method adds all the items of the list to the original set. - If you call the
discard()
method with a value that doesn’t exist in the set, it does nothing. No error; it’s just a no-op.Here’s the difference: if the value doesn’t exist in the set, theremove()
method raises aKeyError
exception. - The difference of two sets is not symmetric. That makes sense; it’s analogous to subtracting one number from another. The order of the operands matters.
- Any set with at least one item is true. The value of the items is irrelevant.
- Dictionaries aren’t just for strings. Dictionary values can be any datatype, including integers, booleans, arbitrary objects, or even other dictionaries. And within a single dictionary, the values don’t all need to be the same type; you can mix and match as needed. Dictionary keys are more restricted, but they can be strings, integers, and a few other types. You can also mix and match key datatypes within a dictionary.
None
is a special constant in Python. It is a null value.None
is not the same asFalse
.None
is not 0.None
is not an empty string. ComparingNone
to anything other thanNone
will always returnFalse
.None
is the only null value. It has its own datatype (NoneType
). You can assignNone
to any variable, but you can not create otherNoneType
objects. All variables whose value isNone
are equal to each other.