已采纳回答 / 刚子曰
文档字符串。注意,是 __doc__ ,前后各两个下划线。一般而言,是对函数/方法/模块所实现功能的简单描述。但当指向具体对象时,会显示此对象从属的类型的构造函数的文档字符串。(示例见以下 a.__doc__)>>> str.__doc__"str(string[, encoding[, errors]]) -> str\n\nCreate a new string object from the given encoded string.\nencoding defaults t...
2016-07-20
def f1(x, y):
return x * y
def calc_prod(lst):
def lazy_prod():
return reduce(f1,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
return x * y
def calc_prod(lst):
def lazy_prod():
return reduce(f1,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-07-19
cmp和key:http://blog.csdn.net/harry_haiwei/article/details/49616409
2016-07-19
class Person():
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
不加object
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
不加object
2016-07-19
import __future__
s = 'am I an unicode?'
print isinstance(s, unicode)
这样就不对?
s = 'am I an unicode?'
print isinstance(s, unicode)
这样就不对?
2016-07-19
def performance(unit):
def performance_dec(f):
def fn(*args, **kw):
t1 = time.clock()
res = f(*args, **kw)
print 'call %s() in %f %s' % (f.__name__, (time.clock() - t1)*1000, unit)
return res
return fn
return performance_dec
def performance_dec(f):
def fn(*args, **kw):
t1 = time.clock()
res = f(*args, **kw)
print 'call %s() in %f %s' % (f.__name__, (time.clock() - t1)*1000, unit)
return res
return fn
return performance_dec
2016-07-19
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except AttributeError:
print 'attributeerror'
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except AttributeError:
print 'attributeerror'
2016-07-19
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = [p2,p1,p3]
print L2[0].name
print L2[1].name
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = [p2,p1,p3]
print L2[0].name
print L2[1].name
print L2[2].name
2016-07-19
应该是说,在外部是可以修改__count属性的,但是不能访问,所以在Class外面打Print Person.__count会报错,但是如果在里面写一个打印的命令的话会看到__count是有加一的
2016-07-18