def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
2015-11-07
class Person(object):
address = 'Earth'
def __init__(self, name):
self.name = name
p1 = Person('Apple')
p2 = Person('Bob')
print p1.address
p1.address = 'Ocean'
print p1.address
print Person.address
可见类实例的属性,当对象实例化时,对象可以访问到类属性,但是这个类属性,是和当前对象绑定到一起的,对象对类属性的修改不影响类本身的类属性,只影响对象拷贝的那一份类属性。
address = 'Earth'
def __init__(self, name):
self.name = name
p1 = Person('Apple')
p2 = Person('Bob')
print p1.address
p1.address = 'Ocean'
print p1.address
print Person.address
可见类实例的属性,当对象实例化时,对象可以访问到类属性,但是这个类属性,是和当前对象绑定到一起的,对象对类属性的修改不影响类本身的类属性,只影响对象拷贝的那一份类属性。
2015-11-05
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print p.__score
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print p.__score
2015-11-05
class Person(object):
def __init__(self, name, gender, birth, **kwargs):
self.name = name
self.gender = gender
self.birth = birth
for key, value in kwargs.items():
setattr(self, key, value)
def __init__(self, name, gender, birth, **kwargs):
self.name = name
self.gender = gender
self.birth = birth
for key, value in kwargs.items():
setattr(self, key, value)
2015-11-05
*args是一般的普通参数, 实际传入的是一个元组,如(a,b), **kwargs是关键字参数,用于传key=value这样的参数
2015-11-05
class A(object)
pass
a1 = A()
a2 = A()
1.判断某个对象是否是类的实例
isinstance(a1, A)
2.判断两个对象是否是否完全一致(意思为: 判断两个变量引用的地址是否相同,如果相同,则两个变量实际引用为同一对象)
用is进行判断
a3 = a1
print a1 is a3
3.判断两个对象中的内容是否相同
用 == 进行判断
print a1 == a2
但是此时结果为 false
表示不解
pass
a1 = A()
a2 = A()
1.判断某个对象是否是类的实例
isinstance(a1, A)
2.判断两个对象是否是否完全一致(意思为: 判断两个变量引用的地址是否相同,如果相同,则两个变量实际引用为同一对象)
用is进行判断
a3 = a1
print a1 is a3
3.判断两个对象中的内容是否相同
用 == 进行判断
print a1 == a2
但是此时结果为 false
表示不解
2015-11-05
import os.path
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
2015-11-05
import math
def sqr(x):
return x and math.sqrt(x)%1==0
print filter(sqr,range(1,101))
def sqr(x):
return x and math.sqrt(x)%1==0
print filter(sqr,range(1,101))
2015-11-05
def f(x):
return x[:1].upper() + x[1:].lower()
print map(f,['adam', 'LISA', 'barT'])
return x[:1].upper() + x[1:].lower()
print map(f,['adam', 'LISA', 'barT'])
2015-11-05