# 1
class A:
def say_something(self):
print("A says something")
class B:
def print_info(self):
print("This is B")
class C:
def say_something(self):
print("C says something")
def print_info(self):
print("This is C")
class D(B):
pass
class E:
pass
class G(E, C):
pass
# A, B, E, C
class F(A, B, G):
def say_something(self):
super(E, self).say_something()
f = F()
f.print_info()
f.say_something()
print(F.__mro__)
# 2
def sum_outer():
data_list = []
def sum_inner(arg):
data_list.append(arg)
print(sum(data_list))
return sum_inner
sum_func = sum_outer()
sum_func(10)
sum_func(20)
#3
import time
def cost_time(func):
def inner(*args, **kwargs):
before_time = time.time()
func(*args, **kwargs)
after_time = time.time()
print(after_time - before_time)
return inner
print(time.time())
@cost_time
def timein():
time.sleep(2)
print("timein")
timein()
# 4
class tody:
def __init__(self):
self._data = None
@staticmethod
def static_method(*args, **kwargs):
print("this is staticm")
@classmethod
def class_method(cls):
print("this is classm")
@property#访问data方法,转为属性:只提供访问:xxx.peoperty
def data(self):
return self._data
@data.setter#委托属性:提供修改:xxx.setter
def data(self, value):
self._data = value
@data.deleter#委托属性:删除:xxx.deleter(xxx根据property决定)
def data(self):
del self._data
m = tody()
tody.class_method()
tody.static_method()
m.class_method()
m.static_method()
print(m.data)
m.data = "data"
print(m.data)
# del m.data
# print(m.data)
运行结果:
?
|