The resource loading... loading...

python: Please be careful in these places.

Author: Ilidhan, Created: 2016-12-29 13:46:12, Updated:

python: Please be careful in these places.

python是一门非常有趣的语言。它提供了许多非常方便的标准库和许多内置命令是我们轻松完成任务.但是好东西太多了就有选择恐惧症了,以至于我们不能很好第利用这个标准库和它的基本机构。下面列出了一些对python新手来说很简单有效的陷阱。

  • Ignore the version of Python

    This is an issue that people keep coming up with on StackOverflow. It's a good time to check if your versions of Python are the same. Make sure your code is running on the version of Python you know.

    $ python --version
    Python 2.7.9
    

    Python version management

    pyenv is a good python version controller. Unfortunately it only runs on *nix systems. On Mac OS, you can simply install pyenv with brew install.

  • I'm stuck with one line of code to solve all the problems.

    Many people boast that I have solved all the problems with one line of code, even if their code is less efficient than normal writing, and the code is more difficult to read and even ambiguous.

    l = [m for a, b in zip(this, that) if b.method(a) != b for m in b if not m.method(a, b) and reduce(lambda x, y: a + y.method(), (m, a, b))]
    

    To be honest, the code above was written by me to illustrate this. But I've actually seen a lot of people do it. If you just show up to solve a complex problem by simply adding something to a list or a set, you may not get paid.

    One line of code control is not a huge accomplishment, although it can sometimes seem particularly clever. Good code is concise but focuses more on efficiency and readability.

  • Initialized set incorrectly

    这是一个更加微妙的问题,有时候会让你措手不及。set推导式起来有点像list推导式.

    >>> { n for n in range(10) if n % 2 == 0 }
    {0, 8, 2, 4, 6}
    >>> type({ n for n in range(10) if n % 2 == 0 })
    

    The example above illustrates this. ⇒ set is a bit like putting a list in a container. The difference between them is that the set has no repeating values and is unordered. {} is usually thought of as an empty set, but it is not, it is an empty dict.

    >>> {}
    {}
    >>> type({})
    

    So if we want to initialize an empty set, we can just use the set ((()

    >>> set()
    set()
    >>> type(set())
    

    Note that an empty set can be represented as a set (((), but is a set containing elements that is defined as a set (([1, 2])).

  • Misunderstanding the GIL

    GIL (global interpreter lock) means that only one thread in a Python program can run at any time. This means that when we can't create a thread and expect it to run in parallel, the Python interpreter actually does a quick switch between different running threads. But this is a very simple version.

    Many people will try to justify to Python that these are all real threads.3. This is true, but it doesn't change the fact that Python handles threads differently than you would expect. Ruby has a similar situation ((and an interpreter lock)).

    The proposed solution is to use multiprocessing modules. The processes provided by multiprocessing modules can basically cover the differences quite well. However, the differences are much higher than the cost of the threads.

    However, this is not a problem that every Python program encounters. PyPy-stm is an example of a Python implementation that is not affected by GIL. Implementations built on other platforms such as JVM (Jython) or CLR (IronPython) do not have GIL issues.

    In short, be careful when using thread classes, you may not get what you want.

  • Use of outdated style classes

    There are two types of classes in Python 2, the old-style class and the new-style class. If you are using Python 3, you are using the default class. To ensure that you are using the new-style class in Python 2, you need to inherit object or any new class you create that does not always inherit the built-in instruction int or list. In other words, your base class should always inherit object.

    class MyNewObject(object): # stuff here
    

    These new class boxes fix some very basic problems that were present in the old class boxes, if you're interested you can check the documentation.

  • The wrong iteration

    The following mistakes are very common for newcomers:

    for name_index in range(len(names)):
      print(names[name_index])
    

    Obviously, it is not necessary to use len, and actually going through the list can be done with a very simple sentence:

    for name in names:
       print(name)  
    

    In addition, there are a bunch of other tools you can use to simplify your iteration. For example, zip can be used to go through two lists:

    for cat, dog in zip(cats, dogs):
       print(cat, dog)
    

    If we want to consider index and value list variables, we can use enumerate.

    for index, cat in enumerate(cats):
       print(cat, index)
    

    There are many other features to choose from in ittools. If ittools has a feature you want, it's easy to get it, but don't use it too much for the sake of using it.

    itertools abuse caused a big god on StackOverflow to take a long time to fix it.

    The default parameters are used

    I've seen a lot of these:

    def foo(a, b, c=[]):
       # append to c
       # do some more stuff
    

    Do not use variable default parameters, instead use:

    def foo(a, b, c=None):
     if c is None:
       c = []
       # append to c
       # do some more stuff 
    

    The following example will help us to understand this very intuitively:

    In[2]: def foo(a, b, c=[]):
    ...     c.append(a)
    ...     c.append(b)
    ...     print(c)
    ...
    In[3]: foo(1, 1)
    [1, 1]
    In[4]: foo(1, 1)
    [1, 1, 1, 1]
    In[5]: foo(1, 1)
    [1, 1, 1, 1, 1, 1]
    

    The same c is referenced over and over again every time the function is called. This can have some very unwanted consequences.


More