"/><script>alert(document.cookie)</script><!--
<script>alert(document.cookie)</script><!--
"onclick="alert(document.cookie)
<script>alert(document.cookie)</script><!--
"onclick="alert(document.cookie)
2015-09-23
初学者定义__init__()方法常常忘记了 self 参数:
>>> class Person(object):
... def __init__(name, gender, birth):
>>> class Person(object):
... def __init__(name, gender, birth):
2015-09-22
由于Python是动态语言,类中的属性不必预先定义。对每一个实例,都可以直接给他们的属性赋值。每个实例可以拥有各自不同的属性,使用属性和普通变量一样。
2015-09-22
return -1 if (self.score > s.score or (self.score==s.score and self.name<s.name)) else 1
2015-09-22
class Fib(object):
def __call__(self, num):
num1, num2, L = 0, 1, []
for n in range(num):
L.append(num1)
num1, num2 = num2, num1 + num2
return str(L)
f = Fib()
print f(10)
def __call__(self, num):
num1, num2, L = 0, 1, []
for n in range(num):
L.append(num1)
num1, num2 = num2, num1 + num2
return str(L)
f = Fib()
print f(10)
2015-09-22
def gcd(self,p,q):
if p < q:
p ^= q
q ^= p
p ^= q
while q:
tmp = p % q
p = q
q = tmp
return p
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
if p < q:
p ^= q
q ^= p
p ^= q
while q:
tmp = p % q
p = q
q = tmp
return p
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
2015-09-22
def __cmp__(self, s):
if isinstance(s,Student):
return cmp(self.name,s)
else:
return cmp(self.name,str(s))
L = [Student('Tim', 99), Student('Bob', 88), 100, 'Hello']
print sorted(L)
if isinstance(s,Student):
return cmp(self.name,s)
else:
return cmp(self.name,str(s))
L = [Student('Tim', 99), Student('Bob', 88), 100, 'Hello']
print sorted(L)
2015-09-22
def __cmp__(self, s):
return -1 if self.score > s.score or (self.score == s.score and self.name < s.name) else 0 if self.score == s.score and self.name == s.name else 1
return -1 if self.score > s.score or (self.score == s.score and self.name < s.name) else 0 if self.score == s.score and self.name == s.name else 1
2015-09-22
def __cmp__(self, s):
return -1 if self.score > s.score else 0 if self.score == s.score else 1
return -1 if self.score > s.score else 0 if self.score == s.score else 1
2015-09-22