Python:常用高级语法

01_property属性_装饰器方式

class Person(object):
    def __init__(self):
        self.__age = 0

    # 获取属性
    @property
    def age(self):
        return self.__age

    # 修改属性
    @age.setter
    def age(self, new_age):
        self.__age = new_age

# p = Person()
# age = p.age()
# print(age)

p = Person()
print(p.age)
# 修改属性
p.age = 100
print(p.age)

02_property属性_类属性方式

class Person(object):
    def __init__(self):
        self.__age = 0

    def get_age(self):
        """当获取age属性时会使用该方法"""
        return self.__age

    def set_age(self, new_age):
        """当设置属性时会使用该方法"""
        if new_age >= 150:
            print("年龄错误")
        else:
            self.__age = new_age

    age = property(get_age, set_age)

p = Person()
print(p.age)
# 设置属性
p.age = 100
print(p.age)

03_with语句

with open("1.txt", "r") as f:
    file_data = f.read()
    print(file_data)

04_上下文管理器

# 1定义一个File类
class File(object):
    def __init__(self, file_name, file_model):
        self.file_name = file_name
        self.file_model = file_model

# 2实现 __enter__() 和 __exit__()方法
    def __enter__(self):
        print("这是上文")
        self.file = open(self.file_name, self.file_model)
        return self.file

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("这是下文")
        self.file.close()

# 3然后使用 with 语句来完成操作文件
with File("1.txt","r") as f:
    file_data = f.read()
    print(file_data)


05_生成器推导式


06_yield关键字

def generater(num):
    for i in range(num):
        print("开始")
        yield i
        print("生成完成")

g = generater(5)
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
for i in g:
    print(i)

07_斐波那契数列

# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
def fb(num):
    a = 0
    b = 1

    # 记录生成了几个数字
    index = 0

    while index < num:
        result = a
        #1  1      1   1
        #1  2      1   2
        #2  3      2   3
        a, b = b, a+b
        yield result
        index += 1

f = fb(5)
# print(next(f))
# print(next(f))
for i in f:
    print(i)

08_浅拷贝

import copy

# 1 普通赋值
# a = [1, 2, 3]
# b = [11, 22, 33]
# c = [a, b]
#
# d = c
#
# print(id(d))
# print(id(c))

# 2 浅拷贝可变类型
# a = [1, 2, 3]
# b = [11, 22, 33]
# c = [a, b]
#
# d = copy.copy(c)
#
# print(id(d))
# print(id(c))

# 3 浅拷贝-深层数据
# a = [1, 2, 3]
# b = [11, 22, 33]
# c = [a, b]
#
# d = copy.copy(c)
#
# print(id(a))
# print(id(c[0]))
# print(id(d[0]))

# 4 浅拷贝不可变类型
a = (1, 2, 3)
b = (11, 22, 33)
c = (a, b)

d = copy.copy(c)

print(id(d))
print(id(c))

09_深拷贝

import copy

# 深拷贝可变类型
# a = [1, 2, 3]
# b = [11, 22, 33]
# c = [a, b]
#
# d = copy.deepcopy(c)
#
# print(id(c))
# print(id(d))

# 深拷贝-深层数据
# a = [1, 2, 3]
# b = [11, 22, 33]
# c = [a, b]
#
# d = copy.deepcopy(c)
#
# print(id(a))
# print(id(c[0]))
# print(id(d[0]))


# 深拷贝不可变类型
a = (1, 2, 3)
b = (11, 22, 33)
c = (a, b)

d = copy.deepcopy(c)

print(id(c))
print(id(d))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值