好像是不太正确的,装饰器,我的理解是以原函数为参数,再定义一个新函数,然后调用原函数。返回的是一个新函数。这样新函数既有了原函数的一切功能,又增加了新的功能。调用装饰器函数得到新函数,调用新函数就可以得到你想要的了。这是我的理解,理解不多,还请多多指教。
2018-01-26
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-01-26
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print p3.count
count = 0
def __init__(self, name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print p3.count
2018-01-26
lass Person(object):
def __init__(self, name, score):
self.name = name
self.score = score
self.__score = 90
p = Person('Bob', 59)
print p.name
try:
print p.__score
except AttributeError:
print 'AttributeError'
#使用异常处理,真是棒棒的
def __init__(self, name, score):
self.name = name
self.score = score
self.__score = 90
p = Person('Bob', 59)
print p.name
try:
print p.__score
except AttributeError:
print 'AttributeError'
#使用异常处理,真是棒棒的
2018-01-25
def __init__(self,name,gender,birth,**kw):
self.name = name
self.gender= gender
self.birth = birth
for k in kw:
setattr(self,k,kw[k])
self.name = name
self.gender= gender
self.birth = birth
for k in kw:
setattr(self,k,kw[k])
2018-01-25
Python3:
L2 = sorted(L1,key = lambda x:x.name,reverse=False)
L2 = sorted(L1,key = lambda x:x.name,reverse=False)
2018-01-25
print sorted(['bob', 'about', 'Zoo', 'Credit'],key=lambda x:x[:1].lower(),reverse=False)
2018-01-25
AttributeError: 'function' object has no attribute '_name_'
不知道是哪儿有问题 还请不吝赐教
不知道是哪儿有问题 还请不吝赐教
2018-01-25
class Fib(object):
def __init__(self):
pass
def __call__(self,n):
L = [0,1]
for i in range(2,n):
L.append(L[i-2]+L[i-1])
return L
f = Fib()
print f(10)
def __init__(self):
pass
def __call__(self,n):
L = [0,1]
for i in range(2,n):
L.append(L[i-2]+L[i-1])
return L
f = Fib()
print f(10)
2018-01-25
def __init__(self, num):
L = [0,1]
for i in range(2,num):
s = L[i-2]+L[i-1]
L.append(s)
self.L = L
def __str__(self):
return str(self.L)
def __len__(self):
return len(self.L)
L = [0,1]
for i in range(2,num):
s = L[i-2]+L[i-1]
L.append(s)
self.L = L
def __str__(self):
return str(self.L)
def __len__(self):
return len(self.L)
2018-01-25
isinstance 判断对象的类型
>>> isinstance(1, int)
True
>>> isinstance(1.0, float)
True
>>> isinstance(1, int)
True
>>> isinstance(1.0, float)
True
2018-01-25
class Person(object):
pass
xiaoming = Person()
xiaohong = Person()
print isinstance(xiaoming,Person)
print isinstance(xiaohong,Person)
print xiaoming is xiaohong
pass
xiaoming = Person()
xiaohong = Person()
print isinstance(xiaoming,Person)
print isinstance(xiaohong,Person)
print xiaoming is xiaohong
2018-01-25
def calc_prod(lst):
return lambda:reduce((lambda x,y: x*y),lst)
f = calc_prod([1, 2, 3, 4])
print f()
return lambda:reduce((lambda x,y: x*y),lst)
f = calc_prod([1, 2, 3, 4])
print f()
2018-01-25
class Person(object):
def __init__(self,name,gender,birth,**kw):
self.name = name
self.gender = gender
self.birth = birth
for key in kw:
setattr(self, key, kw[key])
def __init__(self,name,gender,birth,**kw):
self.name = name
self.gender = gender
self.birth = birth
for key in kw:
setattr(self, key, kw[key])
2018-01-25