#在迭代器和可迭代序列上进行迭代,还能把他们转换成序列 (实现了__iter__方法的对象是可迭代的,实现了next方法对象则是迭代器)
fibs = Fibs()
for f in fibs:
if f > 10000:
print f
break
>>>1597
Python迭代器使用(yield)
回溯法求解8皇后问题
#争端函数,问题描述:如何在棋盘上放置8个皇后,使其不会相互攻击
def conflict(state, nextX):
nextY = len(state)
for i in range(nextY):
if abs(state -nextX) in (0, nextY - i):
return True
return False
#回溯求解问题
def queens(num=8, state = ()):
for pos in range(num):
if not conflict(state, pos):
if len(state) == num - 1:
yield (pos, )
else:
for result in queens(num, state + (pos, )):
yield (pos, ) + result
#include <string.h>
int is_palindrome(char *text){
int i = 0;
int n = strlen(text);
int ret = 1;
for(i = 0; i <= n/2; i++){
if(text != text[n-i-1]){
ret = 0;
break;
}
}
return ret;
}
还需要动手写一个文件(palindrome.i)
该文件只需要声明导出的所有的函数(和变量)即可。
除此之外,头部的一个单元(通过%{和}%来分界)内,可以指定包含头文件(比如本例中的string.h)以及在这之前的一个%module声明,即为模块定义一个名字。
%module palindrome
%{
#include <string.h>
%}
extern int is_palindrome(char *text);
Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity (currently implemented as its address). An object’s type is also unchangeable. [1] An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The type() function returns an object’s type (which is an object itself). The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.
Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable
help> id
Help on built-in function id in module __builtin__:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
该函数返回的对象的内存地址值,通过该函数的声明不难发现i也是对象。
细心的读者可能会发现,此处发生了内存泄露,就i在指向10以后,对象5就不在被任何对象所引用。
不用担心,python的开发者早就想好处理方法,即对象的垃圾回收机制。
我们继续讨论int class 的详细情况
help> int
Help on class int in module __builtin__:
class int(object)
| int(x[, base]) -> integer
|
| Convert a string or number to an integer, if possible. A floating point
| argument will be truncated towards zero (this does not include a string
| representation of a floating point number!) When converting a string, use
| the optional base. It is an error to supply a base when converting a
| non-string. If base is zero, the proper base is guessed based on the
| string content. If the argument is outside the integer range a
| long object will be returned instead.
|
| Methods defined here:
|
| __abs__(...)
| x.__abs__() <==> abs(x)
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __and__(...)