class Person(object):
def __init__(self,name,age,sex,*args):
self.name = nam
self.age = age
self.sex = sex
self.args = args
xiaoming = Person("xiaoming",12,"male","teacher","police")
print xiaoming.name
print xiaoming.sex
print xiaoming.args
def __init__(self,name,age,sex,*args):
self.name = nam
self.age = age
self.sex = sex
self.args = args
xiaoming = Person("xiaoming",12,"male","teacher","police")
print xiaoming.name
print xiaoming.sex
print xiaoming.args
2015-10-03
import math
def is_sqr(x):
return x and math.sqrt(x)== int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x and math.sqrt(x)== int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2015-10-02
class Person(object):
def __init__(self,name,gender,birth,job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
def __init__(self,name,gender,birth,job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2015-10-01
已采纳回答 / PengCheng
先看看有理数的定义:在数学上,有理数是一个整数a和一个非零整数b的比,例如3/8,通则为a/b,故又称作分数。0也是有理数,也是整数。有理数是整数和分数的集合,整数亦可看做是分母为一的分数。你贴的Rational(self.p * r.q + self.q * r.p, self.q * r.q)代码是 class Rational(object)里的 __add__方法 用下面这个例子来解释r1 = Rational(1, 3)r2 = Rational(1, 2)print r1+r2首先r1 ...
2015-10-01
'call %s() in %fs' % (f.__name__,(t2-t1)) 这个%s() 和 %fs' 没看懂
2015-09-28
第二种方法,
for k,v in kw.items():
setattr(self,k,v)
_______________这个函数可以实现的功能也是把**kw,字典里面的k,v赋值进来,self还是找到那个p1或者说该题里面的xiaoming
上面的内容都是
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
for k,v in kw.items():
setattr(self,k,v)
_______________这个函数可以实现的功能也是把**kw,字典里面的k,v赋值进来,self还是找到那个p1或者说该题里面的xiaoming
上面的内容都是
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
2015-09-26
这个题的关键就是解决**kw,字典的引入,有两个办法
第一个,for k,v in kw.items():
stm='self.%s=\'%s\''%(k,v)
exec(stm)
#exec(),执行字符串或者文件中的python语句,self为自己定义的一个名字,就像上一节讲义里的p1一样,k,v分别是key,value的缩写,原来的内容里提到过这种用法,可以提取dic,字典里面的关键字和内容
第一个,for k,v in kw.items():
stm='self.%s=\'%s\''%(k,v)
exec(stm)
#exec(),执行字符串或者文件中的python语句,self为自己定义的一个名字,就像上一节讲义里的p1一样,k,v分别是key,value的缩写,原来的内容里提到过这种用法,可以提取dic,字典里面的关键字和内容
2015-09-26
def calc_prod(lst):
def prod(a,b):
return a*b
def my_prod():
return reduce(prod,lst)
return my_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(a,b):
return a*b
def my_prod():
return reduce(prod,lst)
return my_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-09-26
def cmp_ignore_case(s1, s2):
if s1.capitalize() > s2.capitalize():
return 1
if s1.capitalize() < s2.capitalize():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.capitalize() > s2.capitalize():
return 1
if s1.capitalize() < s2.capitalize():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-09-26
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2015-09-26
第三种:
L1 = [p1, p2, p3]
L2=sorted(L1,lambda x,y:cmp(x.name,y.name))
print L2[0].name
print L2[1].name
print L2[2].name
L1 = [p1, p2, p3]
L2=sorted(L1,lambda x,y:cmp(x.name,y.name))
print L2[0].name
print L2[1].name
print L2[2].name
2015-09-26
第二种:L2=sorted(L1,key=lambda m:m.name)
print L2[0].name
print L2[1].name
print L2[2].name
print L2[0].name
print L2[1].name
print L2[2].name
2015-09-26