最近在研读Python源码剖析一书,此书相当不错,如果自己冲动的去分析Python源码可能会到处碰“鼻”,看到此书时是09年,那时为了研究内存机制才发现有这么一本书,但是工作太忙,根本没时间去分析源码,到了2010年,这是非常有深重意义的一年,所以这一年一定要比之前做的还要付出更多,要想成为技术顶尖就必须研读别人的优化源码,而且必须要全面剖析,而正在此时Python源码剖析又有一次偶然的机会让我碰见了,那也是由于公司组织购书,后来才想起来,不过被同事提出来,原来志同道合的人还真多,所以被抢先借走了,就在最近终于可以借上这本书了,感觉超爽,因为在广州购书中心根本找不到这本书,而且再加上当时也在研究工作内容细节,所以就更没时间了,在研究此书过程中,虽然有部分文字和代码有误,但是总体来说,发现实现Python的作者真是太伟大了,这里面有C的精华之处,有很优美的算法,有面向对象的思想,有设计模式的影子,有编译原理的知识,实在太多了,希望Python开发者或C/C++程序员也好,研究这份源码确实对提高功力有非常大的帮助,Fighting! ......
0、字符串是不可变的。
1、基本字符串操作:索引、切片、复制、成员、长度、最大和最小。
2、字符串格式化:用格式化操作符百分号%实现,eg:>>> format = "Hello, %s. %s enough for ya?"
>>> values = ('world', 'Hot') //元组
&nb ......
在一个项目中需要获取随机数,谁知道遇到点问题:随机数不随机。所以我写了个简单原型。看下到底是啥问题。
import os,random,sys,time
while True:
father = os.fork()
if father:
time.sleep(2)
rd = 7
else:
#random.seed()
rd = random.choice([2,3,4,5])
print rd
time.sleep(2)
sys.exit(0)
代码基本就这样。谁知道在子进程里面打印的 random 不起作用。每次随机数都是一样的。
测试发现。是 while 出现问题,因为while 一直循环,而随机种子,是在第一次 import random 的时候就已经种下了。所以导致随机数一直都不会变化~~~ 汗。幼稚问题导致浪费很多时间
看random.seed手册解释:
If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported.
明白了。最后用 random.seed() 来改变随机种子。。 ......
1。
myCoolVariable="some_string"
os.system("echo myCoolVariable")
2.
>>> os.system('echo "asdg"')
asdg
0
>>> os.system("echo 'asdgwere'")
asdgwere
0
3.
$ python
>>>hamburger="potato"
>>>import os
>>>os.system("echo 'hamburger'")
potato
0
4.
hamburger=os.system('echo $MY_SHELL_VAR | sed "s/longsed/replacement/g"')
5.
>>> string="python is cool"
>>> cmd="echo "+string
>>> os.system(cmd)
python is cool
0
>>>
6.
bash$ python
Python 2.4.3 (#1, May 18 2006, 07:40:45)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import system
>>> food = 'spam'
>>> system('echo %(food)s' % locals())
spam
0
>>>
......
用gcc编译了一个C++调用python的程序,这个程序在VS下是好用的,而且没有使用vs的任何库
可是到了gcc下就是无法使用
后来上网查了一下资料才知道,是因为cl与gcc的运行时库不同。
打开cmd窗口,输入python就可以看到
Python 3.0 (r30:67507, Dec 3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
python的windows版本是用MSC编译的
所以python.lib中的运行时库与gcc无法匹配
解决办法,当然最先想到的是下一份python源代码用gcc再编译一遍。试了一下,觉得工作量很大,时间来不及,就没有仔细研究。
最后只好安装了一个Cygwin了,安装的时候选择了附带的python包、perl包。 ......
今天学习了一下Python的操作符重载,总结了几点比较神奇的东东:
------------------------------------------------------------------------------------------------------------
关于iter:
Technically, iteration contexts work by calling the iter built-in function to try to
find an _ _iter_ _ method, which is expected to return an iterator object. If it’s
provided,Python then repeatedly calls this iterator object’s next method to produce
items until a StopIteration exception is raised. If no such _ _iter_ _ method is found,
Python falls back on the _ _getitem_ _ scheme, and repeatedly indexes by offsets as
before, until an IndexError exception is raised.
所以为了使用iter,我们必须重载__iter__,然后再定义一个next方法,例子如下:
class Squares:
def _ _init_ _(self, start, stop): # Save state when created
self.value = start - 1
self.stop = stop
def _ _iter_ _(self): # Get iterator object on iter( )
return self
def n ......