class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
2018-02-28
class Person(object):
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)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
。。。
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)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
。。。
2018-02-28
def fib(self,num):
L = []
n,a,b = 0,0,1
while n < num-1:
if n == 0:
L.append(a)
L.append(b)
a,b = b,a+b
n += 1
return L
def __call__(self,x):
return str(self.fib(x))
L = []
n,a,b = 0,0,1
while n < num-1:
if n == 0:
L.append(a)
L.append(b)
a,b = b,a+b
n += 1
return L
def __call__(self,x):
return str(self.fib(x))
2018-02-27
class Student(Person):
__slots__ = ('score')
def __init__(self,name,gender,score):
Person.__init__(self,name,gender)
# super(Student, self).__init__(name, gender)
self.score = score
__slots__ = ('score')
def __init__(self,name,gender,score):
Person.__init__(self,name,gender)
# super(Student, self).__init__(name, gender)
self.score = score
2018-02-27
#创造属性
@property
def grade(self):
if self.score >= 80:
return 'A'
elif self.score >= 60:
return 'B'
return 'C'
@property
def grade(self):
if self.score >= 80:
return 'A'
elif self.score >= 60:
return 'B'
return 'C'
2018-02-27
def __str__(self):
n = self.gcd(self.p,self.q)
return '%s/%s' % (self.p/n,self.q/n)
def gcd(self,n1,n2):
if n1%n2 == 0:
return n2
return self.gcd(n2,n1%n2)
n = self.gcd(self.p,self.q)
return '%s/%s' % (self.p/n,self.q/n)
def gcd(self,n1,n2):
if n1%n2 == 0:
return n2
return self.gcd(n2,n1%n2)
2018-02-27
def __str__(self):
return str(self.fab(self.num))
def __len__(self):
return len(self.fab(self.num))
return str(self.fab(self.num))
def __len__(self):
return len(self.fab(self.num))
2018-02-27
def __init__(self, num):
self.num = num
def fab(self,count):
L = []
n,a,b = 0,0,1
while n < count-1:
if n== 0:
L.append(a)
L.append(b)
a,b = b,a+b
n += 1
return L
self.num = num
def fab(self,count):
L = []
n,a,b = 0,0,1
while n < count-1:
if n== 0:
L.append(a)
L.append(b)
a,b = b,a+b
n += 1
return L
2018-02-27
def cmp_ignore_case(s1, s2):
return 1 if s1.lower() > s2.lower() else -1 if s1.lower() < s2.lower() else 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return 1 if s1.lower() > s2.lower() else -1 if s1.lower() < s2.lower() else 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-02-27
def __str__(self):
return '(Student: %s, %s, %s)' % (self.name,self.gender,self.score)
return '(Student: %s, %s, %s)' % (self.name,self.gender,self.score)
2018-02-27
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.items():
setattr(self,k,v)
self.name = name
self.gender = gender
for k,v in kw.items():
setattr(self,k,v)
2018-02-27
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.iteritems():
setattr(Person,k,v)
self.name = name
self.gender = gender
for k,v in kw.iteritems():
setattr(Person,k,v)
2018-02-27