Comments (5)
搞 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
总结来说也就几句话的事情:
- 一个模块不能尝试把自己放到 top level import (PYTHONPATH)中
- 一个模块如果想要被被别的包 import 的话,那么它应该在 top level (PYTHONPATH)中
- 让一个模块能够在其他模块中直接正确引用应该是 项目的工作(而不是模块的工作)
- 接着第三条: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.
- 最最最最重要的事: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.
参考
- PEP 366 -- Main module explicit relative imports
- PEP 328 -- Imports: Multi-Line and Absolute/Relative
摘录
- 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 ...
- 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.
from blog.
关于列表解析式(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 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.
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.
Generators vs Iterables vs Iterators
直接看这篇文章吧,多看几遍就明白了:
http://nvie.com/posts/iterators-vs-generators/
from blog.
Related Issues (20)
- My curious-list
- python 好吃的语法糖 HOT 1
- 我认同的健身 HOT 2
- git 问题汇总
- daily issue HOT 1
- 让你彻底明白 export 和 import
- 奖励模板 HOT 6
- Love sentence
- 祷告
- From 相亲
- 思维误区
- 如何避免能量的损耗 HOT 13
- 如何訓練反應力,瞬間回答任何問題!
- 两学一做 服务育人 相关文章汇总 HOT 4
- 从京东闪付和白条闪付说起
- 10月21日
- 机器密码备忘
- end of story
- 分析下分手后的自我
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.