python笔记-基础-07

格式化 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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值