说实话,没怎么看懂,我现在学到40课了,我再学几课在回头看看吧
修饰符 我可不可以认为就是函数的调用呢{:9_218:}
第三个内置修饰符,看不懂,代码复制下来,运行一遍还报错了。
AttributeError: type object 'Hello' has no attribute 'print_hello'
非常不错!
我看了原版的文章我看懂了,看你这个版本的云里雾里不知所云
版本升级了,感觉有点
看不懂
没看懂。。
完全看不懂,写了什么东西。。
什么鬼,最后三个一带而过?
看不懂
这样不是更加简洁吗?
import time
def timeslong():
start = time.clock()
print("It's time starting ! ")
def f():
print("It's time ending ! ")
end = time.clock()
return "It's used : %s ." % (end - start)
return f()
print(timeslong())
还有,这样也行!!把 @timeslong 去掉 ,末尾加f = timeslong(f)() ,,注意:末尾是:print(f)
import time
def timeslong(func):
def call():
start = time.clock()
print("It's time starting ! ")
func()
print("It's time ending ! ")
end = time.clock()
return "It's used : %s ." % (end - start)
return call
def f():
y = 0
for i in range(10):
y = y + i + 1
print(y)
return y
f = timeslong(f)()
print(f)
aluominhai 发表于 2016-12-9 13:33
还有,这样也行!!把 @timeslong 去掉 ,末尾加f = timeslong(f)() ,,注意:末尾是:print(f)
import ...
由此可见,@timeslong 这个修饰符估计是用来替代 f = timeslong(f)() ,,如果有3层嵌套,比如:
def timeslong(func):
def call():
def add():
那么,@timeslong就是替代f = timeslong(f)()(),,,,假如是4层,就f = timeslong(f)()()()...。是不是这样理解嘛{:10_282:}{:10_266:}
看懂是看懂了
但除了代码简洁了一点外还有什么大用处呢》
哈哈哈哈哈哈哈哈啊哈哈哈哈
import time
def func():
y = 0
for i in range(10):
y = y + i + 1
print(y)
return y
def timeslong(func):
start = time.clock()
print("It's time starting ! ")
func()
print("It's time ending ! ")
end = time.clock()
return "It's used : %s ." % (end - start)
这不就行了? 为什么会要内嵌函数?
import time
def timeslong(func):
def call():
start = time.clock()
print("It's time starting ! ")
func()
print("It's time ending ! ")
end = time.clock()
return "It's used : %s ." % (end - start)
return call
@timeslong
def f():
y = 0
for i in range(10):
y = y + i + 1
print(y)
return y
print(f())
结合了外面看的资料和这里的东西,那个func应该是指的是f()函数吧?那个timeslong是一个装饰器,相当于把f()函数当做一个变量传入到了timeslong里面去了? 然后在里面调用了func()函数即f()....
{:10_257:}
晕乎乎
好不容易看懂了,但是并不明白有什么用{:10_285:}
#!/usr/bin/env python
# coding=utf-8
__metaclass__ = type
class StaticMethod:
@staticmethod
def foo():
print "This is static method foo()."
class ClassMethod:
@classmethod
def bar(cls):
print "This is class method bar()."
print "bar() is part of class:", cls.__name__
if __name__ == "__main__":
static_foo = StaticMethod() #实例化
static_foo.foo() #实例调用静态方法
StaticMethod.foo() #通过类来调用静态方法
print "********"
class_bar = ClassMethod()
class_bar.bar()
ClassMethod.bar()
tttb 发表于 2017-2-1 10:54
说白了就是静态方法和类方法不用实例化就可以调用 不知道为啥起这么多名字