#coding: utf-8
class Person(object):
def get_grade(self):
grade = 'A-优秀';
if self.__score < 60:
grade = 'C-不及格'
elif self.__score < 85:
grade = 'B-及格'
return grade;
class Person(object):
def get_grade(self):
grade = 'A-优秀';
if self.__score < 60:
grade = 'C-不及格'
elif self.__score < 85:
grade = 'B-及格'
return grade;
2017-08-14
class Person(object):
__count = 0
def __init__(self, name):
self.name = name;
Person.__count += 1
print Person.__count,
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
__count = 0
def __init__(self, name):
self.name = name;
Person.__count += 1
print Person.__count,
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
2017-08-14
把self.get_grade 换成x ,把lambda:'A' 换成def y(): return 'A',上面就变成x = y,打印时候x() = y(),第一个返回函数,第二个返回值
2017-08-14
from os.path import isdir, isfile
print isdir(r'/data/webroot/resource')
print isfile(r'/data/webroot/resource/python/test.txt')
print isdir(r'/data/webroot/resource')
print isfile(r'/data/webroot/resource/python/test.txt')
2017-08-14
def calc_prod(lst):
def lazy_calc_prod():
sum = 1
for i in lst:
sum *= i
return sum
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_calc_prod():
sum = 1
for i in lst:
sum *= i
return sum
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
2017-08-14
def performance(unit):
def print_time(f):
def times(*args,**kw):
print 'call '+f.__name__+'()' ,time.time(),unit
return f(*args,**kw)
return times
return print_time
def print_time(f):
def times(*args,**kw):
print 'call '+f.__name__+'()' ,time.time(),unit
return f(*args,**kw)
return times
return print_time
2017-08-14
from functools import cmp_to_key
def cmp_ignore_case(s1, s2):
if s1.lower() >s2.lower() :
return 1
if s1.lower() <s2.lower() :
return -1
return 0
print (sorted(['bob', 'about', 'Zoo', 'Credit'], key=cmp_to_key(cmp_ignore_case)))
def cmp_ignore_case(s1, s2):
if s1.lower() >s2.lower() :
return 1
if s1.lower() <s2.lower() :
return -1
return 0
print (sorted(['bob', 'about', 'Zoo', 'Credit'], key=cmp_to_key(cmp_ignore_case)))
2017-08-14
def cmp_ignore_case(s1, s2):
a1 = s1.upper()
a2 = s2.upper()
if a1 < a2:
return -1
if a1 > a2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
a1 = s1.upper()
a2 = s2.upper()
if a1 < a2:
return -1
if a1 > a2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2017-08-14
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))
2017-08-14
return '(Student:%s,%s,%s)'%(self.name,self.gender,self.score)
__repr__=__str__
__repr__=__str__
2017-08-14