Coder Social home page Coder Social logo

python 知识点总结 about blog HOT 5 OPEN

yqfang avatar yqfang commented on September 14, 2024
python 知识点总结

from blog.

Comments (5)

yqfang avatar yqfang commented on September 14, 2024

搞 python 的模块引入快一天了(一直在用 node 的思维定式在思考python的模块化)。
遇到我本来以为很简单的事情:
https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python
https://stackoverflow.com/questions/6465549/import-paths-the-right-way

总结来说也就几句话的事情:

  1. 一个模块不能尝试把自己放到 top level import (PYTHONPATH)中
  2. 一个模块如果想要被被别的包 import 的话,那么它应该在 top level (PYTHONPATH)中
  3. 让一个模块能够在其他模块中直接正确引用应该是 项目的工作(而不是模块的工作)
  4. 接着第三条:There are two ways to do this. The first is by creating a bootstrapper file such as main.py in the project folder. The other, is by creating a file that adds all relevant paths to PYTHONPATH, that is loaded by any entry points that may exist.
  5. 最最最最重要的事:The main thing to take away, is that a module is not supposed to put itself on the path. The path should be determined automatically by the entry point into the program, or defined explicitly by a setup script that knows where all the relevant modules are.

参考

摘录

  1. Relative imports must always use from <> import ; import <> is always absolute. Of course, absolute imports can use from <> import by omitting the leading dots. The reason import .foo is prohibited is because after ...
  2. Relative Imports and name
    Relative imports use a module's name attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to 'main') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system. 这个解释了为什么在存在相对路径的地方不能作为程序执行入口,因为这样的话 name 为 main,相对 import 相对 main 没有找到任何包的可能。

from blog.

yqfang avatar yqfang commented on September 14, 2024

image

from blog.

yqfang avatar yqfang commented on September 14, 2024

关于列表解析式(list comprehension)

『functional programming python』书中有一段话:Using comprehensions is often a way both to make code more com‐ pact and to shift our focus from the “how” to the “what.” 其实的 what 是指『如何用一堆逻辑去构造一个数据集合』,how 是指:『这个集合由什么构成』

A compre‐ hension is an expression that uses the same keywords as loop and conditional blocks, but inverts their order to focus on the data rather than on the procedure. 这个其实就是函数式编程的**。

列如下面这段代码是我们习以为常的代码风格:

collection = list()
    for datum in data_set:
        if condition(datum): 
            collection.append(datum)
        else:
            new = modify(datum)
            collection.append(new)

用了列表表达式后是这样的:

collection = [d if condition(d) else modify(d) for d in data_set]

Far more important than simply saving a few characters and lines is the mental shift enacted by thinking of what collection is, and by avoiding needing to think about or debug “What is the state of collection at this point in the loop?”

-轻松学会Python列表解析式

python 2.6 中 set comprehensive 和 dict comprehensive 有点瓜三

map = {'hello': 1, 'word': 2} 
反转map:

flapped_map = dict((value, key) for key, value in map.items() )
把 hello 写入集合

sett = set(c for c in 'hello')

from blog.

yqfang avatar yqfang commented on September 14, 2024

Generators comprehension

Generator comprehensions have the same syntax as list comprehen‐ sions, but they are also lazy. That is to say that they are merely a description of how to get the data that is not realized until one explicitly asks for it, either by calling .next() on the object, or by looping over it. This often saves memory for large sequences and defers computation until it is actually needed. For example:

log_lines = (line for line in read_line(huge_log_file) if complex_condition(line))

from blog.

yqfang avatar yqfang commented on September 14, 2024

Generators vs Iterables vs Iterators

直接看这篇文章吧,多看几遍就明白了:

http://nvie.com/posts/iterators-vs-generators/

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.