def calc_prod(lst):
def list_dot():
s=1
for i in lst:
s=s*i
return s
return list_dot
f = calc_prod([1, 2, 3, 4])
print f()
def list_dot():
s=1
for i in lst:
s=s*i
return s
return list_dot
f = calc_prod([1, 2, 3, 4])
print f()
2018-05-02
import math
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
2018-05-02
def cmp_ignore_case(s1, s2):
if s1.upper() < s2.upper():
return -1
if s1.upper() > s2.upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper() < s2.upper():
return -1
if s1.upper() > s2.upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-05-01
class Person(object):
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError as e:
print 'attributeerror'
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError as e:
print 'attributeerror'
2018-05-01
__slots__的目的是限制当前类所能拥有的属性,如果不需要添加任意动态的属性,使用__slots__也能节省内存。
2018-05-01
通过标记一个 @classmethod,该方法将绑定到 Person 类上,而非类的实例。类方法的第一个参数将传入类本身,通常将参数名命名为 cls,上面的 cls.count 实际上相当于 Person.count。
2018-04-30