import json
class Students(object):
def __init__(self, *args):
self.read()
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
class Students(object):
def __init__(self, *args):
self.read()
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
2017-08-13
from functools import reduce
def calc_prod(lst):
def lazy_prod():
def prod(x,y):
return x * y
return reduce(prod, lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print(f())
def calc_prod(lst):
def lazy_prod():
def prod(x,y):
return x * y
return reduce(prod, lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print(f())
2017-08-12
def cmp_ignore_case(s1, s2):
s3 = s1.lower()
s4 = s2.lower()
if s4 > s3:
return -1
if s4 < s3:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
s3 = s1.lower()
s4 = s2.lower()
if s4 > s3:
return -1
if s4 < s3:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2017-08-12
import math
def jisuan(n):
return math.sqrt(n)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9,jisuan)
def jisuan(n):
return math.sqrt(n)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9,jisuan)
2017-08-12
import math
def is_sqr(x):
return str(math.sqrt(x)).split(".")[-1] == "0"
print filter(is_sqr, range(1, 101))
这也是一个思路。
def is_sqr(x):
return str(math.sqrt(x)).split(".")[-1] == "0"
print filter(is_sqr, range(1, 101))
这也是一个思路。
2017-08-12
print最后加逗号就可以控制结尾不打印换行
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count = Person.__count + 1
print Person.how_many(),
p1 = Person('Bob')
print Person.how_many()
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count = Person.__count + 1
print Person.how_many(),
p1 = Person('Bob')
print Person.how_many()
2017-08-11
这一课有点扯淡 跨度大并且是为了举例而举例
花了半小时 写出了适用于Python 3的容易理解的程序 望大家给出建议
def count():
fs = []
for i in range(1, 4):
def f(i):
return(i*i)
fs.append(f(i))
return(fs)
print(count())
这样一来 输出很简洁 简单易懂
花了半小时 写出了适用于Python 3的容易理解的程序 望大家给出建议
def count():
fs = []
for i in range(1, 4):
def f(i):
return(i*i)
fs.append(f(i))
return(fs)
print(count())
这样一来 输出很简洁 简单易懂
2017-08-11
浓缩来说就是factorial=performance(unit)(factorial)(*args,**kw)
print factorial(10)
ms为第一个参使用,返回log_decorator传入factorial函数为参,wrapper ruturn f(10)
print factorial(10)
ms为第一个参使用,返回log_decorator传入factorial函数为参,wrapper ruturn f(10)
2017-08-11
@慕数据5257446 这些只是很简单的小例子,将来自己写大型程序的时候也许像@property这样的属性就派上用场了
2017-08-11