Yuan Yijun (bbbush) wrote,
Yuan Yijun

  • Mood:
Python Tutorial 摘录,第九章 class
1. most container objects can be looped over using a for statement:
2. This style of access is clear, concise, and convenient. The use of iterators pervades and unifies Python.
3. Behind the scenes, the for statement calls iter() on the container object. The function returns an iterator object that defines the method next() which accesses elements in the container one at a time. When there are no more elements, next() raises a StopIteration exception which tells the for loop to terminate.
4. Having seen the mechanics behind the iterator protocol, it is easy to add iterator behavior to your classes. Define a __iter__() method which returns an object with a next() method. If the class defines next(), then __iter__() can just return self:
5. Generators are a simple and powerful tool for creating iterators.
6. They are written like regular functions but use the yield statement whenever they want to return data.
7. Anything that can be done with generators can also be done with class based iterators as described in the previous section. What makes generators so compact is that the __iter__() and next() methods are created automatically.
8. Another key feature is that the local variables and execution state are automatically saved between calls. This made the function easier to write and much more clear than an approach using instance variables like self.index and self.data.

也就是说,在 c++ 里面可以用几种方式来做这件事情:static function variable, function object, 这里可以用简单的办法表达出来,并且几乎所有可以 iterate on 的列表都是遵守这样写法的。一致性是最大的好处,清楚地表达 “自然数”这样的概念是另一个好处。
c++ 的 std::vector 目标也是提供一致的用法,能不能这样简单?觉得取决于重新分配空间之后,iterator 是否会同时更新。应该是可以的。
def fib(n):
    a, b=0,1
    while b<n:
        yield b
        a, b = b, a+b

怎么把这个函数改造成 generator expression?

LiveJournal 要是允许自定义两个 div 标记就好了。现在习惯了 <div style="margin-left:10%;margin-left:10%"/> 和 <div style="margin-left:15%;margin-left:15%"><code></code></div> 来引用文本和代码,觉得比用 &nbsp; 和 <blockquote/> 要好得多。这几天 LJ 速度很慢,不知道为什么?

update: 今天发现 lincomet ersaclarke 的地方,觉得总是把自己描述得很。。。脆弱似的也不好。正如 nq 那位说的一样,太脆弱不好,虽然其他地方可以接受。说起来什么样子的人我最喜欢呢。。。。我喜欢和奇怪的人打交道,例如 trent 那样的。trent 现在生活幸福而甜蜜阿。。。。就好像 Larry Wall 那么幸福了。
to ersaclarke: 去 linuxfans 的 GNOME 版面,那里有我们这一辈的值得佩服的人们。FundaWang,sunmoon1997,xLoneStar,yangh,jcome, 当然还有很多潜水的家伙,和在其他版面作项目的家伙,就像 oneleaf, 华华他们。linuxsir 现在吵架的少了很多,大家都去做正事了。我还在这里混日子吧,直到买了自己的电脑为止。


Tell me what you see
Was it a dream? Was I in it?
Life seemed so obscene until it's over
Who knows?

All I want is someone who can fill the hole
In the life I know
In between life and death when there's nothing left
Do you wanna know?

You come in on your own
And you leave on your own
Forget the lovers you've know
And your friends on your own

Tell me if it's true
That I need you, you are changing
I've seen this road before down on the floor
It is hurting me

All I want is someone who can fill the hole in the life I know
In between life and death when there's nothing left
Do you want to know?

You come in on your own
And you leave on your own
Forget the lovers you've know
And your friends on your own

Lies I've got to get rid of this whole inside (repeat)

来自李冬那张精选 CD,不知道他是否还记得?
今天突然想起来,到 google 搜了一下歌词

今天翻完了一本 python tutorial,觉得比大学里时候看书要快一点了,记忆倒不一定记得住,反正开着两个 cygwin + ipython,不停的看 help。奇怪的是 ipython 不支持好多东西例如翻页,例如智能提示可用的模块以及函数参数类型之类。如果 ipython 或者 eclipse-pydev 可以支持这两项,那岂不是太方便了?话说回来,已有的 help 其实就不错。关键还是理解逻辑,因为对 email、codecs、gc、xmlrpclib、urllib2 这些模块的应用不明白,所以即使它们设计得再巧妙,理解起来也很难。要是知道大概的处理步骤,就像看 CppSQLite 那样知道必须打开数据库才能操作,每个要编译的stmt必须基于已有的数据库,这样的先决条件搞清楚,用起来真是很容易的事情。谁能告诉我这个 email 模块操作的究竟是什么?!
(让人头疼的 COM 据说有很简单的逻辑。夏中伟说,一个 IDL,一个 IUnknown 接口,一个 factory 就可以了,我还得再学学。)

字符串里面,u 和 U 前缀是一样的(将来 python3000 不要这个前缀就好了),字符串内部的 \uxxxx 和 \Uxxxxxxxx 反而是有区别的,挺混淆。总觉得 python 保留的垃圾成分太多,包括字符串前缀,冒号等等——没有冒号的 python 也一样可以解析对不对?还有 pass 和 self 这两个词,真是让人恨得牙疼 (要是 javascript 那样恐怕又得使用相同的作用域规则吧?)

Tags: 转载

  • My tweets

    Wed, 09:34: WIthout threading support, Outlook managed to introduce "Focused" and "Other" as tabs in the inbox. Now I got two inbox to check.

  • My tweets

    Tue, 11:06: With IntelliJ IDEA it is really hard to tell which window is "active" by looking at the taskbar icon. The project n……

  • My tweets

    Sun, 21:01: Scared a little bit https://t.co/MQI4IPzfDa Sun, 22:07: https://t.co/22J8hD4xrp ugly paradise

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 1 comment