Dive into python3 重点摘录(chapter 1-2)

本文介绍了Python函数的返回值、参数传递规则、模块搜索路径、函数的docstring、异常处理机制、列表与元组的操作,以及字典、集合和None的使用。强调了Python中一切都是对象,包括函数、模块和异常,并展示了如何处理未捕获的异常以及如何通过try-except进行错误处理。此外,还提及了不同数据类型的特点,如列表的动态扩展、元组的不可变性、集合的无序性和字典的键值对操作。
摘要由CSDN通过智能技术生成
  1. In fact, every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value
  2. Reading the argument list from left to right, once you have a single named argument, the rest of the arguments must also be named.
  3. 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.
  4. Everything between the triple quotes is the function’s docstring, which documents what the function does. We can look the doc by .__doc__
  5. All functions have a built-in attribute __doc__, which returns the docstring defined in the function’s source code
  6. 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
  7. 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.
  8. n reality, exceptions are implemented as classes, and this raise statement is actually creating an instance of the ValueError class and passing the string 'number must be non-negative' to its initialization method. 
  9. 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.
  10. 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.
  11. try:
        from lxml import etree
    except ImportError:
        import xml.etree.ElementTree as etree
  12. 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.
  13. 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.
  14. 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.
  15. The extend() method takes a single argument, which is always a list, and adds each of the items of that list to a_list.
  16. On the other hand, the append() method takes a single argument, which can be any datatype. Here, you’re calling the append() method with a list of three items.
  17. That’s right: the index() method raises an exception if it doesn’t find the value in the list.
  18. You can use the del statement to delete a specific item from a list.
  19. You can also remove an item from a list with the remove() method.
  20. 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 the pop() method. It will remove that item, shift all the items after it to “fill the gap,” and return the value it removed.
  21. A tuple is an immutable list. A tuple can not be changed in any way once it is created.
  22. 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.
  23. 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.
  24. 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.
  25. 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
  26. In Python, you can use a tuple to assign multiple values at once.
  27. 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.
  28. 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.
  29. The update() method can take objects of a number of different datatypes, including lists. When called with a list, the update() method adds all the items of the list to the original set.
  30. 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, the remove() method raises a KeyError exception.
  31. 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.
  32. Any set with at least one item is true. The value of the items is irrelevant.
  33. 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.
  34. None is a special constant in Python. It is a null value. None is not the same as FalseNone is not 0. None is not an empty string. Comparing None to anything other than None will always return False.
  35. None is the only null value. It has its own datatype (NoneType). You can assign None to any variable, but you can not create other NoneType objects. All variables whose value is None are equal to each other.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值