[Python] Python programming Art

Source: Internet
Author: User
  • Ref = http://www.slideshare.net/wilhelmshen/py-art
  • 1. Enjoy efficient and fun programming with the python programming Art
  • 2. Def Hello (): Print Hello world!
  • 3. What kind of function returns none?
  • 4. No message is the best message.
  • 5. True/1 does not mean successful execution for many experienced programmers.
  • 6. None is the best silence. Although 0 is good, the default return value in python is none.
  • 7. The function cannot return both results and errors.
  • 8. the publisher's design "if open () returns errno" try: fileobj = open (filename) # Although open can return an error, but it cannot be guaranteed that everything is captured. # Is it safer to catch it? Raise T: Raise # Now, raise. The ghost knows what the situation will be. If isinstance (fileobj, file): # The file object is returned correctly... normal Working code... elif isinstance (fileobj,
    INT): # The error code is returned. Run errno = fileobj raise ioerror (errno, OS. strerror (errno) to handle the error. # What else will happen? # Since there are already multiple types of returned data, it is not certain whether it will be out of date # This time it is really unexpected else: Raise ioerror (-1, unknown error)
  • 9. in fact, the C language is the same, because C has no exception mechanism, and thinks that this method of returning everything in python is obviously due to the "excellent style" of C language-this is a misunderstanding, in fact, the idea of C processing exception results is exactly the same as that of Python.
  • 10. python standard interface try: fobj = open (filename) handle T ioerror :... an error occurred while opening the file ...... as long as there is no exception, fobj must be a file object... try: Data = fobj. read () handle T ioerror :... reading failed ...... as long as there is no exception, data must be of the STR type...
  • 11. Principle of Least Surprise
  • 12. Misunderstanding 1 the predictability of function return values does not fully mean that only one return value is returned.
  • 13. Misunderstanding 2 if possible, returning a return value does not mean returning data of the same "type" or based on the same "base class ".
  • 14. In an example, although all returned results are tupleif...: Return username, passwdelse: Return name, age, gender
  • 15. Duck typing
  • 16. file System File: open (...) and file (...) it can be any media, disk/memory file/sound card/nfs/fuse, and other byte stream files: stringio (...) socket file: socket. makefile (...) MPs queue: OS. popen (...) function Family popen (), popen2 (), popen3 (), popen4 () standard input and output: SYS. stdin, sys. stdout, sys. stderr others: gzip. open (..)
    And so on
  • 17. No base class (basefile) is called, but the behavior is consistent.
  • 18. It is very dangerous to apply the idea of static language programming to dynamic languages.
  • 19. Dynamic Language initialization without the need to pre-allocate memory
  • 20. Is it safe to throw notimplementederror from a virtual table?
  • 21. Avoid turning the problem into a logical error
  • 22. interpreter errors are better than runtime errors and try to get errors before the program starts running.
  • 23. Program crash is better than logical errors, trying to turn possible logical errors into crashes
  • 24. In particular, do not capture the best opportunity for attributeerror to discover logical problems
  • 25. Throwing attributeerror and keyerror instead of missing the opportunity by returning none
  • 26. understand the meaning of various Python exceptions. Try to use existing exception types. Python presets a wide range of exception types. In many cases, the design of additional exceptions will be confused with the existing exception types, affecting system stability and increasing maintenance. cost
  • 27. propagation of oserror & ioerrorerrno in Python
  • 28. The bad message is that ioerror is not an oserror. The good news is that socket. error can be obtained through ioerror.
  • 29. typeerror first and then valueerror int (none)/INT (Hello)
  • 30. When you start a large number of raisetypeerror and valueerror, you should pay attention to it.
  • 31. Raise is used for knife makeup.
  • 32. Is asserterror a logical error?
  • 33. Use assert
  • 34. Summary of exception policies do not perform input detection do not write protective Code do not tolerate, so that the program crashes do not handle exceptions, try to create and throw exceptions
  • 35. A vulnerable program? We all know that there is no perfect program, and errors are inevitable. Any small exception can cause the entire program to crash. Is this acceptable for a software product that has started operation?
  • 36. Single Point of Complexity
  • 37. Let professional programs do professional tasks
  • 38. Simple is better
  • 39. inference 1 check will only make the input less reliable
  • 40. inference 2 the more security policies the program will become less secure
  • 41. Only when the error is not an error, which exceptions are solved locally?
  • 42. How to Write fast dict search logic based on different fate
  • 43. The performance of Python exceptions is fast but not as slow as imagined, but not as slow as imagined
  • 44. Return to the duck-typing topic.
  • 45. serious misunderstanding is duck-typing based on object and object methods?
  • 46. If you replace for... in OBJ with obj. _ ITER __()
  • 47. Duck-typing is a, B = AB Based on the call protocol.
  • 48. Call protocol based on function name and Parameter Name
  • 49. Caller (cpython, pypy)
  • 50. Avoid using modifiable objects as the default function parameters.
  • 51. explicitly better than hiding login (username = xxx, password = ***)
  • 52. Expose but not misuse eliminate private and hidden attributes as much as possible expose all interfaces
  • 53. Do not use _ slots _ to make it open and transparent at all levels
  • 54. Repeated Code may not be a bad thing. Give up some encapsulation and let users know exactly what they are doing.
  • 55. Which situations do not require encapsulation?
  • 56. Performance killer Python function calls are heavyweight
  • 57. Horizontal complexity and vertical complexity of thick gluing Layer
  • 58. Do not use super (). When super () is used, the program has failed.
  • 59. Python/Unix First Law Program Complexity is only related to the number of rows
  • 60. Thank you for watching Shen Kun's Python party in Shanghai (5 minutes) February 1, May 30, 2010
  • 61. The art of Python programming has never been absolutely applied.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.