-
strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列,返回的是一个字符串
len()方法返回对象长度或项目个数
len(s.strip()) > 0 的意思就是先去掉字符串s头尾的空格或者换行符,然后看此时返回的字符串的长度是多少。如果此时字符串的长度大于0,则改语句返回True,否则返回False
查看全部 -
除了可以直接使用self.name = 'xxx'设置一个属性外,还可以通过 setattr(self, 'name', 'xxx') 设置属性。
查看全部 -
张腾飞是个臭屌丝查看全部
-
高结函数 ,接收函数为参数,可返回函数
接收一个 函数,包装后,返回一个新的函数
查看全部 -
class Fib(object):
def __call__(self,n):
s=[0,1]
while len(s)<n:
s.append(s[-1]+s[-2])
return s
f = Fib()
print f(10)查看全部 -
class Student(object):
def __init__(self, name, score):
self.name = name
self.__score = score
@property
def func1(self):
return self.__score
@func1.setter
def funct2(self, x):
if x< 0 or x > 100:
raise ValueError('invalid score')
self.__score = x
@property
def grade(self):
if self.func1>=80:
return "A"
elif self.func1>=60:
return "B"
else:
return "C"
s = Student('Bob', 59)
print s.grade
s.funct2= 60
print s.grade
s.funct2= 99print s.grade
这样更好理解点,答案里面总是写相同的名字,针对新手来说反而更不容易理解,会联想相同名字的关联,实际没一点关系
查看全部 -
class Student(object):
def __init__(self, name, score):
self.name = name
self.__score = score
@property
def score(self):
return self.__score
@score.setter
def score(self, score):
if score < 0 or score > 100:
raise ValueError('invalid score')
self.__score = score
@property
def grade(self):
if self.score>=80:
return "A"
elif self.score>=60:
return "B"
else:
return "C"
s = Student('Bob', 59)
print s.grade
s.score = 60
print s.grade
s.score = 99
print s.grade查看全部 -
def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
class Rational(object):
def __init__(self, p, q):
self.p = p
self.q = q
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
def __sub__(self, r):
return Rational(self.p * r.q - self.q * r.p, self.q * r.q)
def __mul__(self, r):
return Rational(self.p * r.p ,self.q * r.q)
def __div__(self, r):
return Rational(self.p * r.q ,self.q * r.p)
def __str__(self):
g=gcd(self.p,self.q)
return '{}/{}'.format(self.p/g,self.q/g)
__repr__ = __str__
r1 = Rational(1, 2)
r2 = Rational(1, 4)
print r1 + r2
print r1 - r2
print r1 * r2
print r1 / r2查看全部 -
class Fib(object):
def __init__(self, num):
self.num=num
self.s=[0,1]
while len(self.s)<self.num:
self.s.append(self.s[-1]+self.s[-2])
def __str__(self):
return str(self.s)
__repr__=__str__
def __len__(self):
return len(self.s)
f = Fib(10)
print f
print len(f)查看全部 -
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def __str__(self):
return '(%s: %s)' % (self.name.lower(), self.score)
__repr__ = __str__
def __cmp__(self, s):
if self.score>s.score:
return -1
elif self.score<s.score:
return 1
else:
if self.name.lower()<s.name.lower():
return -1
elif self.name.lower()>s.name.lower():
return 1
L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]
print sorted(L)def __cmp__(self, s): if self.score == s.score: return cmp(self.name, s.name) return -cmp(self.score, s.score)
查看全部 -
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
def __str__(self):
return "(Student:{},{},{})".format(self.name,self.gender,self.score)
__repr__=__str__
s = Student('Bob', 'male', 88)
print s查看全部 -
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Teacher(Person):
def __init__(self, name, gender, course):
Person.__init__(self,name,gender)
self.course=course
t = Teacher('Alice', 'Female', 'English')
print t.name
print t.course查看全部 -
class Person(object):
__count = 0
def __init__(self,name):
self.name=name
Person.__count+=1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()查看全部 -
#coding=utf-8
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
def get_grade(self):
if self.__score>=90:
return "A-优秀"
elif self.__score>=60:
return "B-及格"
else:
return "C-不及格"
p1 = Person('Bob', 90)
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()查看全部 -
import functools
sorted_ignore_case = functools.partial(sorted,cmp=lambda s1,s2:cmp(s1.upper(),s2.upper()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])查看全部
举报