老师,我打python出现的那天开始就一直在编程,这点知识点不在话下,你信不?不信?不信你咋不给人家多写一些讲解~
2018-01-26
好像是不太正确的,装饰器,我的理解是以原函数为参数,再定义一个新函数,然后调用原函数。返回的是一个新函数。这样新函数既有了原函数的一切功能,又增加了新的功能。调用装饰器函数得到新函数,调用新函数就可以得到你想要的了。这是我的理解,理解不多,还请多多指教。
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
最新回答 / qq_路过曾经_0
这个函数分为两步执行第一步:执行外层函数 cal_prod,该函数返回值是一个内部函数第二步:执行内部函数,内部调用reduce方法,返回计算后的值
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
最新回答 / 慕神1935635
我觉得课程里那样写的逻辑是这样的:首先第一步:定义一个装饰函数log<...code...>然后第二步:定义fn,把定义过程放到上面的........中去。两种写法应该得到的结果是一样的。
2018-01-25