-
在定义继承类的时候,有几点是需要注意的:
class Student()定义的时候,需要在括号内写明继承的类Person
在__init__()方法,需要调用super(Student, self).__init__(name, gender),来初始化从父类继承过来的属性
查看全部 -
#!/usr/bin/env python # coding: utf-8 """ @author ChenDehua 2021/4/22 19:31 @note """ import time def log(timeunit): def wrapper(f): def inner_wrapper(*args, **kwargs): print("current timeunit is {}".format(timeunit)) start = time.time() print("start time -> {}".format(start)) result = f(*args, **kwargs) time.sleep(1) end = time.time() print("end time -> {} spend time :{}" .format(end, end - start if timeunit == "s" else (end * 1000) - (start * 1000))) return result return inner_wrapper return wrapper @log("ms") def register_student(name,age,**kwargs): print("register_student --> name:{} age:{} additional info:{}" .format(name, age, "".join(["{}:{} ".format(key, value) for key,value in kwargs.items()]))) if __name__ == "__main__": register_student("chendehua", 21, class_no=1800502108, school="gdpu")
查看全部 -
print([item for item in sorted(['bob', 'about', 'Zoo', 'Credit'], key=lambda value:value[0].lower())])
一行代码搞定
查看全部 -
一行搞定:
print([item for item in filter(lambda x:int(math.sqrt(x)) * int(math.sqrt(x))== x, [i for i in range(1,100)])])
查看全部 -
if __name__ == "__main__": with open("file.txt", 'a+') as file: file.seek(0) lines = file.readlines() file.write("\n") for line in lines: file.write(line)
查看全部 -
if __name__ == "__main__": file = open("file.txt", "r") line2 = file.readlines() new_file = open("file1.txt", 'w') for index, line in enumerate(line2): line = line[-2::-1] if index != len(line2) - 1 else line[::-1] new_file.write(line + "\n" if index != len(line2) - 1 else line) new_file.close() file.close()
这才是正确答案
查看全部 -
例子:运行时导入上级目录的模块
if __name__ == "__main__":
import sys
sys.path.append("../")
import hello
hello.say_hello()查看全部 -
关于多重继承,需要彻底弄懂!
有如下继承关系:
class A:
def __init__(self, msg):
print("init A..", msg)
class B(A):
def __init__(self, msg):
super(B, self).__init__(msg)
print("init B..", msg)
class C(A):
def __init__(self, msg):
super(C, self).__init__(msg)
print("init C..", msg)
class D1(C):
def __init__(self, msg):
super(D1, self).__init__(msg)
print("init D1..", msg)
class D(C,B):
def __init__(self, msg):
super(D, self).__init__(msg)
print("init D..", msg)
class Zero:
def __init__(self, msg):
print("init Zero", msg)
class One(Zero):
def __init__(self, msg):
super(One, self).__init__(msg)
print("init One", msg)
class E(One, C):
def __init__(self):
super(C, self).__init__("ddd")class E(One, C):
def __init__(self):
super(C, self).__init__("ddd")super(C, self).__init__("ddd")的意思是找类C的父类(类A)
ddd作为类A的msg:
e = E()
只输出init A.. ddd
如果是super(One, self).__init__("ddd"),代表找One的父类(Zero),输出init Zero ddd(super(One,self)只是找One的父类,并不会执行One的init方法)
如果是super(E, self).__init__("ddd"),则会输出
init Zero ddd
init One ddd找E的父类,此时因为E是多重继承,继承了One,和C,此时super(E,self)就会选择第一个类One,而不会去找C
还有一种情况就是菱形继承:
class Base(object):
def __init__(self):
print("enter Base")
print("leave Base")
class A(Base):
def __init__(self):
print("enter A")
super(A, self).__init__()
print("leave A")
class B(Base):
def __init__(self):
print("enter B")
super(B, self).__init__()
print("leave B")
class C(A, B):
def __init__(self):
print("enter C")
super(C, self).__init__()
print("leave C")
if __name__ == "__main__":
c = C()输出:
enter C
enter A
enter B
enter Base
leave Base
leave B
leave A
leave C看看c的mro就知道调用顺序了
>>> C.mro() # or C.__mro__ or C().__class__.mro()
[__main__.C, __main__.A, __main__.B, __main__.Base, object]不存在菱形继承问题的时候(第一种情况),就可以手动指定需要去调用哪一个类的父类,如果父类是多重继承的,就取第一个类。
class A:
def __init__(self, msg):
print("init A..", msg)
class C(A):
def __init__(self, msg):
super(C, self).__init__(msg)
print("init C..", msg)
class Zero:
def __init__(self, msg):
print("init Zero", msg)
class Zero1:
def __init__(self, msg):
print("init Zero1", msg)
class One(Zero, Zero1):
def __init__(self, msg):
super(One, self).__init__(msg)
print("init One", msg)
class E(One, C):
def __init__(self):
# super(E, self).__init__("ddd")
super(One, self).__init__("ddd")
if __name__ == "__main__":
e = E()所以,super可以调用多次,来选择不同父类的init方法
第二种情况,菱形问题的时候,就需要用到mro,来决定调用顺序
查看全部 -
class Animal(object):
def __init__(self, name, age):#初始化
self.name=name
self.age=age
dog=Animal("曦曦",10)
cat=Animal("欢欢",9)
print(dog.name,dog.age)
查看全部 -
测试查看全部
-
在定义继承类的时候,有几点是需要注意的:
class Student()定义的时候,需要在括号内写明继承的类Person
在__init__()方法,需要调用super(Student, self).__init__(name, gender),来初始化从父类继承过来的属性
查看全部 -
在类属性和实例属性同时存在的情况下,实例属性的优先级是要高于类属性的,在操作实例的时候,优先是操作实例的属性。
可见通过实例是无法修改类的属性的,事实上,通过实例方法修改类属性,只是给实例绑定了一个对应的实例属性。
查看全部 -
r1 = Rational(1, 2) 第一个数是1/2
r2 = Rational(1, 5) 第二个数是1/5
查看全部
举报