@property
def grade(self):
if self.__score >= 80:
return 'A'
return 'B' if self.__score>=60 else 'C'
def grade(self):
if self.__score >= 80:
return 'A'
return 'B' if self.__score>=60 else 'C'
2018-02-06
class Fib(object):
def __init__(self, num):
pass
def __call__(self, num):
L = [0,1]
for i in range(num-2):
L.append(L[-1] + L[-2])
return L
f = Fib(10)
print f(10)
def __init__(self, num):
pass
def __call__(self, num):
L = [0,1]
for i in range(num-2):
L.append(L[-1] + L[-2])
return L
f = Fib(10)
print f(10)
2018-02-06
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.iteritems():
setattr(Person, k, v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.iteritems():
setattr(Person, k, v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2018-02-06
@BUPTGHOST, 这位老哥, 再看一下最上面, print p 和 p 的区别你就知道为什么了, 当然咯, 这里不是Dos环境, 也验证不出来, 不过我觉得你有兴趣可以去试试
2018-02-06
import functools
sorted_ignore_case = functools.partial(sorted,key=str.upper)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,key=str.upper)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-02-06
我认为老师最想表达的是, 因为Python是动态语言, 只有在运行时才做类型检查, 因此只要是同名的函数, 就能够被调用, 而不像C++中, 必须参数个数, 参数顺序, 参数类型相同才可以被特定的函数调用
2018-02-06
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print(Person.__count)
p1 = Person('Bob')
p2 = Person('Alice')
try:
print(Person.__count)
except AttributeError:
print("AttributeError")
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print(Person.__count)
p1 = Person('Bob')
p2 = Person('Alice')
try:
print(Person.__count)
except AttributeError:
print("AttributeError")
2018-02-06
import math
def is_sqr(x):
if math.sqrt(x) - int(math.sqrt(x)) == 0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x) - int(math.sqrt(x)) == 0:
return x
print filter(is_sqr, range(1, 101))
2018-02-05
def count():
fs = []
for i in range(1, 4):
def f(i):
def fn():
return i*i
return fn
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
def fn():
return i*i
return fn
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-02-05