class Person(object):
__count = 0
def __init__(self, name):
self.name=name
Person.__count=Person.__count+1
p1 = Person('Bob')
print p1._Person__count
p2 = Person('Alice')
print p2._Person__count
__count = 0
def __init__(self, name):
self.name=name
Person.__count=Person.__count+1
p1 = Person('Bob')
print p1._Person__count
p2 = Person('Alice')
print p2._Person__count
2017-10-10
class Person(object):
__count = 0
def __init__(self, name):
self.name = name;
Person.__count += 1;
print Person.__count;
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError as error:
print "AttributeError"
__count = 0
def __init__(self, name):
self.name = name;
Person.__count += 1;
print Person.__count;
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError as error:
print "AttributeError"
2017-10-09
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1,f2,f3
改下括号,直接返回值~
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1,f2,f3
改下括号,直接返回值~
2017-10-09
import os
print os.path.isdir(r'/data/webroot')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
print os.path.isdir(r'/data/webroot')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
2017-10-09
import time
def performance(unit):
def fuck(f):
def fuck1(n):
if unit=='s' :
print 'call factirial()',time.time()
elif unit=='ms':
print 'call factirial()',round(time.time()*1000)
return f(n)
return fuck1
return fuck
def performance(unit):
def fuck(f):
def fuck1(n):
if unit=='s' :
print 'call factirial()',time.time()
elif unit=='ms':
print 'call factirial()',round(time.time()*1000)
return f(n)
return fuck1
return fuck
2017-10-08
import functools
def cmp_sorted(x,y):
x = x.upper()
y = y.upper()
if x > y:
return 1
if x < y:
return -1
if x ==y:
return 0
def sorted_ignore_case(lst):
return sorted(lst,cmp_sorted)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
def cmp_sorted(x,y):
x = x.upper()
y = y.upper()
if x > y:
return 1
if x < y:
return -1
if x ==y:
return 0
def sorted_ignore_case(lst):
return sorted(lst,cmp_sorted)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2017-10-08
def calc_prod(lst):
def reduce2():
def to(x,y):
return x*y
return reduce(to,lst)
return reduce2
f = calc_prod([1, 2, 3, 4])
print f()
def reduce2():
def to(x,y):
return x*y
return reduce(to,lst)
return reduce2
f = calc_prod([1, 2, 3, 4])
print f()
2017-10-08
def calc_prod(lst):
def multiply():
def f(x,y):
return x*y
return reduce(f,lst)
return multiply
f = calc_prod([1, 2, 3, 4])
print f()
def multiply():
def f(x,y):
return x*y
return reduce(f,lst)
return multiply
f = calc_prod([1, 2, 3, 4])
print f()
2017-10-07
import math
print filter(lambda x : int(math.sqrt(x)) ** 2 == x, range(1,101))
print filter(lambda x : int(math.sqrt(x)) ** 2 == x, range(1,101))
2017-10-06
def format_name(s):
return s.lower().title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.lower().title()
print map(format_name, ['adam', 'LISA', 'barT'])
2017-10-06