格式化 Linting:
def print_hello(name:str):
print('my name is '+name)
print_hello('huang yao hui')
lambda:
presenters=[
{'name':'aaa','age':50},
{'name':'bbb','age':40}
]
presenters.sort(key=lambda parameter_list: parameter_list['age'])
print(presenters)
类 class
class HelloWord():
def __init__(self,name):
self.name=name
def say_hello(self):
print('hello '+self.name)
hello=HelloWord('huang')
hello.say_hello()
property装饰器
# encoding='utf-8'
class HelloWord():
def __init__(self,name):
self.name=name
@property
def name(self):
return self.name
@name.setter
def name(self,name):
self.name=name
def say_hello(self):
print('hello '+self.name)
hello=HelloWord('huang')
hello.say_hello()
继承:
# encoding='utf-8'
class HelloWord():
def __init__(self,name):
self.name=name
def say_hello(self):
print('hello '+self.name)
class HelloYou(HelloWord):
def __init__(self, name,sex):
super().__init__(name)
self.sex=sex
def say_hello_you(self):
print("hello "+self.name+" "+self.sex)
def __str__(self):
return self.name
hello_you=HelloYou('huang','is a good people')
hello_you.say_hello_you()
print(isinstance(hello_you,HelloWord))
print(isinstance(hello_you,HelloYou))
print(issubclass(HelloYou,HelloWord))
print(hello_you)
多重继承:
class A():
def __init__(self):
self.name=''
def show(self):
print(self.name)
class B():
def __init__(self):
self.sex=''
def say(self):
print(self.sex)
class C(A,B):
def __init__(self):
self.name='huang'
self.sex='nan'
def show(self):
super().show();
super().say();
c=C()
c.show()
文件操作:
from pathlib import Path
# 获取当前目录
cwd=Path.cwd()
print(cwd)
new_file=Path.joinpath(cwd,'001.txt')
print(new_file)
parent=cwd.parent
print(parent.is_dir())
print(parent.is_file())
for chile in parent.iterdir():
if chile.is_dir():
print(chile)
demo_file=Path(Path.joinpath(cwd,'demo.txt'))
print(demo_file.name)
print(demo_file.suffix)
print(demo_file.parent.name)
print(demo_file.stat().st_size)