def __cmp__(self, s):
if self.score>s.score:
return -1
if self.score<s.score:
return 1
return cmp(self.name,s.name)
if self.score>s.score:
return -1
if self.score<s.score:
return 1
return cmp(self.name,s.name)
2015-07-25
最赞回答 / 放学路上的小学生
不是的哦。self.k意思是给实例增加了个k属性,这里的k是固定不变的,和name,score什么的没区别。而setattr(self, k, v)里的k和v是变量,是动态变化的。见下图,直接打印 a.k属性,这个属性名称就是k。<...图片...>
2015-07-24
没有学setattr(self, k, v),只有这样
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
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
2015-07-24
try:
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
2015-07-24
from os.path import isdir,isfile
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
2015-07-24
感觉还不懂得来这里,讲得很清楚了
http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html
http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html
2015-07-24
def calc_prod(lst):
def lazy_prod():
n=1
for i in lst:
n*=i
return n
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
n=1
for i in lst:
n*=i
return n
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-07-24
#from __future__ import unicode_literals
s = u'am I an unicode?'
print isinstance(s, unicode)
s = u'am I an unicode?'
print isinstance(s, unicode)
2015-07-23
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2015-07-23
def count():
fs = []
for i in range(1, 4):
fs.append(lambda x=i:x*x)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
fs.append(lambda x=i:x*x)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-07-23